forked from rouge-ruby/rouge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rouge-ruby#606 from germanriano/master
Added lexer for Mosel language.
- Loading branch information
Showing
4 changed files
with
459 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
(!****************************************************** | ||
Mosel Example Problems | ||
*******************************************************!) | ||
|
||
model "A-6 Electricity production" | ||
uses "mmxprs" | ||
|
||
declarations | ||
NT = 7 | ||
TIME = 1..NT ! Time periods | ||
TYPES = 1..4 ! Power generator types | ||
|
||
LEN, DEM: array(TIME) of integer ! Length and demand of time periods | ||
PMIN,PMAX: array(TYPES) of integer ! Min. & max output of a generator type | ||
CSTART: array(TYPES) of integer ! Start-up cost of a generator | ||
|
||
start: array(TYPES,TIME) of mpvar ! No. of gen.s started in a period | ||
work: array(TYPES,TIME) of mpvar ! No. of gen.s working during a period | ||
padd: array(TYPES,TIME) of mpvar ! Production above min. output level | ||
end-declarations | ||
|
||
initializations from 'a6electr.dat' | ||
LEN DEM PMIN PMAX CSTART CMIN CADD AVAIL | ||
end-initializations | ||
|
||
! Objective function: total daily cost | ||
Cost:= sum(p in TYPES, t in TIME) (CSTART(p)*start(p,t) + | ||
LEN(t)*(CMIN(p)*work(p,t) + CADD(p)*padd(p,t))) | ||
|
||
! Number of generators started per period and per type | ||
forall(p in TYPES, t in TIME) | ||
start(p,t) >= work(p,t) - if(t>1, work(p,t-1), work(p,NT)) | ||
|
||
! Limit on power production above minimum level | ||
forall(p in TYPES, t in TIME) padd(p,t) <= (PMAX(p)-PMIN(p))*work(p,t) | ||
|
||
|
||
! Limit number of available generators; numbers of generators are integer | ||
forall(p in TYPES, t in TIME) do | ||
work(p,t) <= AVAIL(p) | ||
work(p,t) is_integer | ||
end-do | ||
|
||
! Solve the problem | ||
minimize(Cost) | ||
|
||
! Solution printing | ||
writeln("Daily cost: ", getobjval) | ||
|
||
end-model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
# -*- coding: utf-8 -*- # | ||
|
||
module Rouge | ||
module Lexers | ||
class Mosel < RegexLexer | ||
tag 'mosel' | ||
filenames '*.mos' | ||
title "Mosel" | ||
desc "An optimization language used by Fico's Xpress." | ||
# http://www.fico.com/en/products/fico-xpress-optimization-suite | ||
filenames '*.mos' | ||
|
||
mimetypes 'text/x-mosel' | ||
|
||
def self.analyze_text(text) | ||
return 1 if text =~ /^\s*(model|package)\s+/ | ||
end | ||
|
||
id = /[a-zA-Z_][a-zA-Z0-9_]*/ | ||
|
||
############################################################################################################################ | ||
# General language lements | ||
############################################################################################################################ | ||
|
||
core_keywords = %w( | ||
and array as | ||
boolean break | ||
case count counter | ||
declarations div do dynamic | ||
elif else end evaluation exit | ||
false forall forward from function | ||
if imports in include initialisations initializations integer inter is_binary is_continuous is_free is_integer is_partint is_semcont is_semint is_sos1 is_sos2 | ||
linctr list | ||
max min mod model mpvar | ||
next not of options or | ||
package parameters procedure | ||
public prod range real record repeat requirements | ||
set string sum | ||
then to true | ||
union until uses | ||
version | ||
while with | ||
) | ||
|
||
core_functions = %w( | ||
abs arctan assert | ||
bitflip bitneg bitset bitshift bittest bitval | ||
ceil cos create currentdate currenttime cuthead cuttail | ||
delcell exists exit exp exportprob | ||
fclose fflush finalize findfirst findlast floor fopen fselect fskipline | ||
getact getcoeff getcoeffs getdual getfid getfirst gethead getfname getlast getobjval getparam getrcost getreadcnt getreverse getsize getslack getsol gettail gettype getvars | ||
iseof ishidden isodd ln log | ||
makesos1 makesos2 maxlist minlist | ||
publish | ||
random read readln reset reverse round | ||
setcoeff sethidden setioerr setname setparam setrandseed settype sin splithead splittail sqrt strfmt substr | ||
timestamp | ||
unpublish | ||
write writeln | ||
) | ||
|
||
############################################################################################################################ | ||
# mmxprs module elements | ||
############################################################################################################################ | ||
|
||
mmxprs_functions = %w( | ||
addmipsol | ||
basisstability | ||
calcsolinfo clearmipdir clearmodcut command copysoltoinit | ||
defdelayedrows defsecurevecs | ||
estimatemarginals | ||
fixglobal | ||
getbstat getdualray getiis getiissense getiistype getinfcause getinfeas getlb getloadedlinctrs getloadedmpvars getname getprimalray getprobstat getrange getsensrng getsize getsol getub getvars | ||
implies indicator isiisvalid isintegral loadbasis | ||
loadmipsol loadprob | ||
maximize, minimize | ||
postsolve | ||
readbasis readdirs readsol refinemipsol rejectintsol repairinfeas resetbasis resetiis resetsol | ||
savebasis savemipsol savesol savestate selectsol setbstat setcallback setcbcutoff setgndata setlb setmipdir setmodcut setsol setub setucbdata stopoptimize | ||
unloadprob | ||
writebasis writedirs writeprob writesol | ||
xor | ||
) | ||
|
||
mmxpres_constants = %w(XPRS_OPT XPRS_UNF XPRS_INF XPRS_UNB XPRS_OTH) | ||
|
||
mmxprs_parameters = %w(XPRS_colorder XPRS_enumduplpol XPRS_enummaxsol XPRS_enumsols XPRS_fullversion XPRS_loadnames XPRS_problem XPRS_probname XPRS_verbose) | ||
|
||
|
||
############################################################################################################################ | ||
# mmsystem module elements | ||
############################################################################################################################ | ||
|
||
mmsystem_functions = %w( | ||
addmonths | ||
copytext cuttext | ||
deltext | ||
endswith expandpath | ||
fcopy fdelete findfiles findtext fmove | ||
getasnumber getchar getcwd getdate getday getdaynum getdays getdirsep | ||
getendparse setendparse | ||
getenv getfsize getfstat getftime gethour getminute getmonth getmsec getpathsep | ||
getqtype, setqtype | ||
getsecond | ||
getsepchar, setsepchar | ||
getsize | ||
getstart, setstart | ||
getsucc, setsucc | ||
getsysinfo getsysstat gettime | ||
gettmpdir | ||
gettrim, settrim | ||
getweekday getyear | ||
inserttext isvalid | ||
makedir makepath newtar | ||
newzip nextfield | ||
openpipe | ||
parseextn parseint parsereal parsetext pastetext pathmatch pathsplit | ||
qsort quote | ||
readtextline regmatch regreplace removedir removefiles | ||
setchar setdate setday setenv sethour | ||
setminute setmonth setmsec setsecond settime setyear sleep startswith system | ||
tarlist textfmt tolower toupper trim | ||
untar unzip | ||
ziplist | ||
) | ||
|
||
mmsystem_parameters = %w(datefmt datetimefmt monthnames sys_endparse sys_fillchar sys_pid sys_qtype sys_regcache sys_sepchar) | ||
|
||
############################################################################################################################ | ||
# mmjobs module elements | ||
############################################################################################################################ | ||
|
||
mmjobs_instance_mgmt_functions = %w( | ||
clearaliases connect | ||
disconnect | ||
findxsrvs | ||
getaliases getbanner gethostalias | ||
sethostalias | ||
) | ||
|
||
mmjobs_model_mgmt_functions = %w( | ||
compile | ||
detach | ||
getannidents getannotations getexitcode getgid getid getnode getrmtid getstatus getuid | ||
load | ||
reset resetmodpar run | ||
setcontrol setdefstream setmodpar setworkdir stop | ||
unload | ||
) | ||
|
||
mmjobs_synchornization_functions = %w( | ||
dropnextevent | ||
getclass getfromgid getfromid getfromuid getnextevent getvalue | ||
isqueueempty | ||
nullevent | ||
peeknextevent | ||
send setgid setuid | ||
wait waitfor | ||
) | ||
|
||
mmjobs_functions = mmjobs_instance_mgmt_functions + mmjobs_model_mgmt_functions + mmjobs_synchornization_functions | ||
|
||
mmjobs_parameters = %w(conntmpl defaultnode fsrvdelay fsrvnbiter fsrvport jobid keepalive nodenumber parentnumber) | ||
|
||
|
||
state :whitespace do | ||
# Spaces | ||
rule /\s+/m, Text | ||
# ! Comments | ||
rule %r((!).*$\n?), Comment::Single | ||
# (! Comments !) | ||
rule %r(\(!.*?!\))m, Comment::Multiline | ||
|
||
end | ||
|
||
|
||
# From Mosel documentation: | ||
# Constant strings of characters must be quoted with single (') or double quote (") and may extend over several lines. Strings enclosed in double quotes may contain C-like escape sequences introduced by the 'backslash' | ||
# character (\a \b \f \n \r \t \v \xxx with xxx being the character code as an octal number). | ||
# Each sequence is replaced by the corresponding control character (e.g. \n is the `new line' command) or, if no control character exists, by the second character of the sequence itself (e.g. \\ is replaced by '\'). | ||
# The escape sequences are not interpreted if they are contained in strings that are enclosed in single quotes. | ||
|
||
state :single_quotes do | ||
rule /'/, Str::Single, :pop! | ||
rule /[^']+/, Str::Single | ||
end | ||
|
||
state :double_quotes do | ||
rule /"/, Str::Double, :pop! | ||
rule /(\\"|\\[0-7]{1,3}\D|\\[abfnrtv]|\\\\)/, Str::Escape | ||
rule /[^"]/, Str::Double | ||
end | ||
|
||
state :base do | ||
|
||
rule %r{"}, Str::Double, :double_quotes | ||
rule %r{'}, Str::Single, :single_quotes | ||
|
||
rule %r{((0(x|X)[0-9a-fA-F]*)|(([0-9]+\.?[0-9]*)|(\.[0-9]+))((e|E)(\+|-)?[0-9]+)?)(L|l|UL|ul|u|U|F|f|ll|LL|ull|ULL)?}, Num | ||
rule %r{[~!@#\$%\^&\*\(\)\+`\-={}\[\]:;<>\?,\.\/\|\\]}, Punctuation | ||
# rule %r{'([^']|'')*'}, Str | ||
# rule /"(\\\\|\\"|[^"])*"/, Str | ||
|
||
|
||
|
||
rule /(true|false)\b/i, Name::Builtin | ||
rule /\b(#{core_keywords.join('|')})\b/i, Keyword | ||
rule /\b(#{core_functions.join('|')})\b/, Name::Builtin | ||
|
||
|
||
|
||
rule /\b(#{mmxprs_functions.join('|')})\b/, Name::Function | ||
rule /\b(#{mmxpres_constants.join('|')})\b/, Name::Constant | ||
rule /\b(#{mmxprs_parameters.join('|')})\b/i, Name::Property | ||
|
||
rule /\b(#{mmsystem_functions.join('|')})\b/i, Name::Function | ||
rule /\b(#{mmsystem_parameters.join('|')})\b/, Name::Property | ||
|
||
rule /\b(#{mmjobs_functions.join('|')})\b/i, Name::Function | ||
rule /\b(#{mmjobs_parameters.join('|')})\b/, Name::Property | ||
|
||
rule id, Name | ||
end | ||
|
||
state :root do | ||
mixin :whitespace | ||
mixin :base | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# -*- coding: utf-8 -*- # | ||
|
||
describe Rouge::Lexers::Mosel do | ||
let(:subject) { Rouge::Lexers::Mosel.new } | ||
|
||
describe 'guessing' do | ||
include Support::Guessing | ||
|
||
it 'guesses by filename' do | ||
assert_guess :filename => 'foo.mos' | ||
end | ||
|
||
it 'guesses by mimetype' do | ||
assert_guess :mimetype => 'text/x-mosel' | ||
end | ||
end | ||
end |
Oops, something went wrong.