Skip to content

Commit dccba78

Browse files
authored
Add AMPL lexer (#1219)
Add support for [AMPL](https://ampl.com/). Largely based on [`pygments.lexers.ampl.AmplLexer`](https://github.com/pygments/pygments/blob/master/pygments/lexers/ampl.py) from Pygments.
1 parent ee4ad0a commit dccba78

File tree

4 files changed

+363
-1
lines changed

4 files changed

+363
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ translators for Pygments lexers and styles.
3636

3737
| Prefix | Language
3838
| :----: | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
39-
| A | ABAP, ABNF, ActionScript, ActionScript 3, Ada, Agda, AL, Alloy, Angular2, ANTLR, ApacheConf, APL, AppleScript, ArangoDB AQL, Arduino, ArmAsm, ATL, AutoHotkey, AutoIt, Awk
39+
| A | ABAP, ABNF, ActionScript, ActionScript 3, Ada, Agda, AL, Alloy, AMPL, Angular2, ANTLR, ApacheConf, APL, AppleScript, ArangoDB AQL, Arduino, ArmAsm, ATL, AutoHotkey, AutoIt, Awk
4040
| B | Ballerina, Bash, Bash Session, Batchfile, Beef, BibTeX, Bicep, BlitzBasic, BNF, BQN, Brainfuck
4141
| C | C, C#, C++, C3, Caddyfile, Caddyfile Directives, Cap'n Proto, Cassandra CQL, Ceylon, CFEngine3, cfstatement, ChaiScript, Chapel, Cheetah, Clojure, CMake, COBOL, CoffeeScript, Common Lisp, Coq, Core, Crystal, CSS, CSV, CUE, Cython
4242
| D | D, Dart, Dax, Desktop file, Diff, Django/Jinja, dns, Docker, DTD, Dylan

lexers/embedded/ampl.xml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<lexer>
2+
<config>
3+
<name>AMPL</name>
4+
<alias>ampl</alias>
5+
<filename>*.mod</filename>
6+
<filename>*.run</filename>
7+
<mime_type>text/x-ampl</mime_type>
8+
<case_insensitive>false</case_insensitive>
9+
<dot_all>true</dot_all>
10+
</config>
11+
<rules>
12+
<state name="root">
13+
<!-- Whitespace -->
14+
<rule pattern="\s+">
15+
<token type="Text"/>
16+
</rule>
17+
18+
<!-- Comments: '#' to end-of-line and C-style comments -->
19+
<rule pattern="#[^\n]*">
20+
<token type="Comment"/>
21+
</rule>
22+
<rule pattern="/\*">
23+
<token type="Comment"/>
24+
<push state="comment"/>
25+
</rule>
26+
27+
<!-- Strings -->
28+
<rule pattern="&quot;(\\.|[^\\&quot;])*&quot;">
29+
<token type="LiteralString"/>
30+
</rule>
31+
<rule pattern="&#39;(\\.|[^\\&#39;])*&#39;">
32+
<token type="LiteralString"/>
33+
</rule>
34+
35+
<!-- Numbers -->
36+
<rule pattern="\b\d+\.\d*([eE][+-]?\d+)?\b">
37+
<token type="LiteralNumberFloat"/>
38+
</rule>
39+
<rule pattern="\b\d+[eE][+-]?\d+\b">
40+
<token type="LiteralNumberFloat"/>
41+
</rule>
42+
<rule pattern="\b\d+\b">
43+
<token type="LiteralNumberInteger"/>
44+
</rule>
45+
46+
<!-- Commands / Reserved keywords -->
47+
<rule pattern="\b(call|cd|close|commands|data|delete|display|drop|end|environ|exit|expand|include|load|model|objective|option|problem|purge|quit|redeclare|reload|remove|reset|restore|shell|show|solexpand|solution|solve|update|unload|xref|coeff|coef|cover|obj|interval|default|from|to|to_come|net_in|net_out|dimen|dimension|check|complements|write|function|pipe|format|if|then|else|in|while|repeat|for)\b">
48+
<token type="KeywordReserved"/>
49+
</rule>
50+
51+
<!-- Types -->
52+
<rule pattern="\b(integer|binary|symbolic|ordered|circular|reversed|INOUT|IN|OUT|LOCAL)\b">
53+
<token type="KeywordType"/>
54+
</rule>
55+
56+
<!-- Declarations (set/param/var etc) -->
57+
<rule pattern="\b(set|param|var|arc|minimize|maximize|subject to|s\.t\.|subj to|node|table|suffix|read table|write table)\b">
58+
<token type="KeywordDeclaration"/>
59+
</rule>
60+
61+
<!-- Builtins / Functions -->
62+
<rule pattern="\b(abs|acos|acosh|alias|asin|asinh|atan|atan2|atanh|ceil|ctime|cos|exp|floor|log|log10|max|min|precision|round|sin|sinh|sqrt|tan|tanh|time|trunc|Beta|Cauchy|Exponential|Gamma|Irand224|Normal|Normal01|Poisson|Uniform|Uniform01|num|num0|ichar|char|length|substr|sprintf|match|sub|gsub|print|printf|next|nextw|prev|prevw|first|last|ord|ord0|card|arity|indexarity)\b">
63+
<token type="NameBuiltin"/>
64+
</rule>
65+
66+
<!-- Operator words (logical/set operators) -->
67+
<rule pattern="\b(or|exists|forall|and|in|not|within|union|diff|difference|symdiff|inter|intersect|intersection|cross|setof|by|less|sum|prod|product|div|mod)\b">
68+
<token type="OperatorWord"/>
69+
</rule>
70+
71+
<!-- Identifiers -->
72+
<rule pattern="[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z0-9_]+)*">
73+
<token type="Name"/>
74+
</rule>
75+
76+
<!-- Operators and punctuation -->
77+
<rule pattern="\+|\-|\*|/|\*\*|=|&lt;=|&gt;=|==|\||\^|&lt;|&gt;|\!|\.\.|:=|\&amp;|\!=|&lt;&lt;|&gt;&gt;">
78+
<token type="Operator"/>
79+
</rule>
80+
<rule pattern="[(),;:\[\]{}.]">
81+
<token type="Punctuation"/>
82+
</rule>
83+
</state>
84+
85+
<state name="comment">
86+
<rule pattern="[^*]+">
87+
<token type="Comment"/>
88+
</rule>
89+
<rule pattern="\*/">
90+
<token type="Comment"/>
91+
<pop depth="1"/>
92+
</rule>
93+
<rule pattern="\*">
94+
<token type="Comment"/>
95+
</rule>
96+
</state>
97+
</rules>
98+
</lexer>

lexers/testdata/ampl.actual

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Simple AMPL example for tests
2+
3+
# Sourced from: https://ampl.com/mo-book/notebooks/01/production-planning-advanced.html
4+
5+
# define sets
6+
set PRODUCTS;
7+
ordered set RESOURCES;
8+
9+
# define parameters
10+
param demand {PRODUCTS} >= 0;
11+
param price {PRODUCTS} > 0;
12+
param available {RESOURCES} >= 0;
13+
param cost {RESOURCES} > 0;
14+
param need {RESOURCES,PRODUCTS} >= 0;
15+
16+
# define variables
17+
var Use {r in RESOURCES} >= 0, <= available[r];
18+
var Sell {p in PRODUCTS} >= 0, <= demand[p];
19+
20+
# define objective function
21+
maximize Profit:
22+
sum {p in PRODUCTS} price[p] * Sell[p] -
23+
sum {r in RESOURCES} cost[r] * Use[r];
24+
25+
# create indexed constraint
26+
subject to ResourceLimit {r in RESOURCES}:
27+
sum {p in PRODUCTS} need[r,p] * Sell[p] <= Use[r];

lexers/testdata/ampl.expected

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
[
2+
{"type":"Comment","value":"## Simple AMPL example for tests"},
3+
{"type":"Text","value":"\n\n"},
4+
{"type":"Comment","value":"# Sourced from: https://ampl.com/mo-book/notebooks/01/production-planning-advanced.html"},
5+
{"type":"Text","value":"\n\n"},
6+
{"type":"Comment","value":"# define sets"},
7+
{"type":"Text","value":"\n"},
8+
{"type":"KeywordDeclaration","value":"set"},
9+
{"type":"Text","value":" "},
10+
{"type":"Name","value":"PRODUCTS"},
11+
{"type":"Punctuation","value":";"},
12+
{"type":"Text","value":"\n"},
13+
{"type":"KeywordType","value":"ordered"},
14+
{"type":"Text","value":" "},
15+
{"type":"KeywordDeclaration","value":"set"},
16+
{"type":"Text","value":" "},
17+
{"type":"Name","value":"RESOURCES"},
18+
{"type":"Punctuation","value":";"},
19+
{"type":"Text","value":"\n\n"},
20+
{"type":"Comment","value":"# define parameters"},
21+
{"type":"Text","value":"\n"},
22+
{"type":"KeywordDeclaration","value":"param"},
23+
{"type":"Text","value":" "},
24+
{"type":"Name","value":"demand"},
25+
{"type":"Text","value":" "},
26+
{"type":"Punctuation","value":"{"},
27+
{"type":"Name","value":"PRODUCTS"},
28+
{"type":"Punctuation","value":"}"},
29+
{"type":"Text","value":" "},
30+
{"type":"Operator","value":"\u003e="},
31+
{"type":"Text","value":" "},
32+
{"type":"LiteralNumberInteger","value":"0"},
33+
{"type":"Punctuation","value":";"},
34+
{"type":"Text","value":"\n"},
35+
{"type":"KeywordDeclaration","value":"param"},
36+
{"type":"Text","value":" "},
37+
{"type":"Name","value":"price"},
38+
{"type":"Text","value":" "},
39+
{"type":"Punctuation","value":"{"},
40+
{"type":"Name","value":"PRODUCTS"},
41+
{"type":"Punctuation","value":"}"},
42+
{"type":"Text","value":" "},
43+
{"type":"Operator","value":"\u003e"},
44+
{"type":"Text","value":" "},
45+
{"type":"LiteralNumberInteger","value":"0"},
46+
{"type":"Punctuation","value":";"},
47+
{"type":"Text","value":"\n"},
48+
{"type":"KeywordDeclaration","value":"param"},
49+
{"type":"Text","value":" "},
50+
{"type":"Name","value":"available"},
51+
{"type":"Text","value":" "},
52+
{"type":"Punctuation","value":"{"},
53+
{"type":"Name","value":"RESOURCES"},
54+
{"type":"Punctuation","value":"}"},
55+
{"type":"Text","value":" "},
56+
{"type":"Operator","value":"\u003e="},
57+
{"type":"Text","value":" "},
58+
{"type":"LiteralNumberInteger","value":"0"},
59+
{"type":"Punctuation","value":";"},
60+
{"type":"Text","value":"\n"},
61+
{"type":"KeywordDeclaration","value":"param"},
62+
{"type":"Text","value":" "},
63+
{"type":"Name","value":"cost"},
64+
{"type":"Text","value":" "},
65+
{"type":"Punctuation","value":"{"},
66+
{"type":"Name","value":"RESOURCES"},
67+
{"type":"Punctuation","value":"}"},
68+
{"type":"Text","value":" "},
69+
{"type":"Operator","value":"\u003e"},
70+
{"type":"Text","value":" "},
71+
{"type":"LiteralNumberInteger","value":"0"},
72+
{"type":"Punctuation","value":";"},
73+
{"type":"Text","value":"\n"},
74+
{"type":"KeywordDeclaration","value":"param"},
75+
{"type":"Text","value":" "},
76+
{"type":"Name","value":"need"},
77+
{"type":"Text","value":" "},
78+
{"type":"Punctuation","value":"{"},
79+
{"type":"Name","value":"RESOURCES"},
80+
{"type":"Punctuation","value":","},
81+
{"type":"Name","value":"PRODUCTS"},
82+
{"type":"Punctuation","value":"}"},
83+
{"type":"Text","value":" "},
84+
{"type":"Operator","value":"\u003e="},
85+
{"type":"Text","value":" "},
86+
{"type":"LiteralNumberInteger","value":"0"},
87+
{"type":"Punctuation","value":";"},
88+
{"type":"Text","value":"\n\n"},
89+
{"type":"Comment","value":"# define variables"},
90+
{"type":"Text","value":"\n"},
91+
{"type":"KeywordDeclaration","value":"var"},
92+
{"type":"Text","value":" "},
93+
{"type":"Name","value":"Use"},
94+
{"type":"Text","value":" "},
95+
{"type":"Punctuation","value":"{"},
96+
{"type":"Name","value":"r"},
97+
{"type":"Text","value":" "},
98+
{"type":"KeywordReserved","value":"in"},
99+
{"type":"Text","value":" "},
100+
{"type":"Name","value":"RESOURCES"},
101+
{"type":"Punctuation","value":"}"},
102+
{"type":"Text","value":" "},
103+
{"type":"Operator","value":"\u003e="},
104+
{"type":"Text","value":" "},
105+
{"type":"LiteralNumberInteger","value":"0"},
106+
{"type":"Punctuation","value":","},
107+
{"type":"Text","value":" "},
108+
{"type":"Operator","value":"\u003c="},
109+
{"type":"Text","value":" "},
110+
{"type":"Name","value":"available"},
111+
{"type":"Punctuation","value":"["},
112+
{"type":"Name","value":"r"},
113+
{"type":"Punctuation","value":"];"},
114+
{"type":"Text","value":"\n"},
115+
{"type":"KeywordDeclaration","value":"var"},
116+
{"type":"Text","value":" "},
117+
{"type":"Name","value":"Sell"},
118+
{"type":"Text","value":" "},
119+
{"type":"Punctuation","value":"{"},
120+
{"type":"Name","value":"p"},
121+
{"type":"Text","value":" "},
122+
{"type":"KeywordReserved","value":"in"},
123+
{"type":"Text","value":" "},
124+
{"type":"Name","value":"PRODUCTS"},
125+
{"type":"Punctuation","value":"}"},
126+
{"type":"Text","value":" "},
127+
{"type":"Operator","value":"\u003e="},
128+
{"type":"Text","value":" "},
129+
{"type":"LiteralNumberInteger","value":"0"},
130+
{"type":"Punctuation","value":","},
131+
{"type":"Text","value":" "},
132+
{"type":"Operator","value":"\u003c="},
133+
{"type":"Text","value":" "},
134+
{"type":"Name","value":"demand"},
135+
{"type":"Punctuation","value":"["},
136+
{"type":"Name","value":"p"},
137+
{"type":"Punctuation","value":"];"},
138+
{"type":"Text","value":"\n\n"},
139+
{"type":"Comment","value":"# define objective function"},
140+
{"type":"Text","value":"\n"},
141+
{"type":"KeywordDeclaration","value":"maximize"},
142+
{"type":"Text","value":" "},
143+
{"type":"Name","value":"Profit"},
144+
{"type":"Punctuation","value":":"},
145+
{"type":"Text","value":"\n "},
146+
{"type":"OperatorWord","value":"sum"},
147+
{"type":"Text","value":" "},
148+
{"type":"Punctuation","value":"{"},
149+
{"type":"Name","value":"p"},
150+
{"type":"Text","value":" "},
151+
{"type":"KeywordReserved","value":"in"},
152+
{"type":"Text","value":" "},
153+
{"type":"Name","value":"PRODUCTS"},
154+
{"type":"Punctuation","value":"}"},
155+
{"type":"Text","value":" "},
156+
{"type":"Name","value":"price"},
157+
{"type":"Punctuation","value":"["},
158+
{"type":"Name","value":"p"},
159+
{"type":"Punctuation","value":"]"},
160+
{"type":"Text","value":" "},
161+
{"type":"Operator","value":"*"},
162+
{"type":"Text","value":" "},
163+
{"type":"Name","value":"Sell"},
164+
{"type":"Punctuation","value":"["},
165+
{"type":"Name","value":"p"},
166+
{"type":"Punctuation","value":"]"},
167+
{"type":"Text","value":" "},
168+
{"type":"Operator","value":"-"},
169+
{"type":"Text","value":"\n "},
170+
{"type":"OperatorWord","value":"sum"},
171+
{"type":"Text","value":" "},
172+
{"type":"Punctuation","value":"{"},
173+
{"type":"Name","value":"r"},
174+
{"type":"Text","value":" "},
175+
{"type":"KeywordReserved","value":"in"},
176+
{"type":"Text","value":" "},
177+
{"type":"Name","value":"RESOURCES"},
178+
{"type":"Punctuation","value":"}"},
179+
{"type":"Text","value":" "},
180+
{"type":"Name","value":"cost"},
181+
{"type":"Punctuation","value":"["},
182+
{"type":"Name","value":"r"},
183+
{"type":"Punctuation","value":"]"},
184+
{"type":"Text","value":" "},
185+
{"type":"Operator","value":"*"},
186+
{"type":"Text","value":" "},
187+
{"type":"Name","value":"Use"},
188+
{"type":"Punctuation","value":"["},
189+
{"type":"Name","value":"r"},
190+
{"type":"Punctuation","value":"];"},
191+
{"type":"Text","value":"\n\n"},
192+
{"type":"Comment","value":"# create indexed constraint"},
193+
{"type":"Text","value":"\n"},
194+
{"type":"KeywordDeclaration","value":"subject to"},
195+
{"type":"Text","value":" "},
196+
{"type":"Name","value":"ResourceLimit"},
197+
{"type":"Text","value":" "},
198+
{"type":"Punctuation","value":"{"},
199+
{"type":"Name","value":"r"},
200+
{"type":"Text","value":" "},
201+
{"type":"KeywordReserved","value":"in"},
202+
{"type":"Text","value":" "},
203+
{"type":"Name","value":"RESOURCES"},
204+
{"type":"Punctuation","value":"}:"},
205+
{"type":"Text","value":"\n "},
206+
{"type":"OperatorWord","value":"sum"},
207+
{"type":"Text","value":" "},
208+
{"type":"Punctuation","value":"{"},
209+
{"type":"Name","value":"p"},
210+
{"type":"Text","value":" "},
211+
{"type":"KeywordReserved","value":"in"},
212+
{"type":"Text","value":" "},
213+
{"type":"Name","value":"PRODUCTS"},
214+
{"type":"Punctuation","value":"}"},
215+
{"type":"Text","value":" "},
216+
{"type":"Name","value":"need"},
217+
{"type":"Punctuation","value":"["},
218+
{"type":"Name","value":"r"},
219+
{"type":"Punctuation","value":","},
220+
{"type":"Name","value":"p"},
221+
{"type":"Punctuation","value":"]"},
222+
{"type":"Text","value":" "},
223+
{"type":"Operator","value":"*"},
224+
{"type":"Text","value":" "},
225+
{"type":"Name","value":"Sell"},
226+
{"type":"Punctuation","value":"["},
227+
{"type":"Name","value":"p"},
228+
{"type":"Punctuation","value":"]"},
229+
{"type":"Text","value":" "},
230+
{"type":"Operator","value":"\u003c="},
231+
{"type":"Text","value":" "},
232+
{"type":"Name","value":"Use"},
233+
{"type":"Punctuation","value":"["},
234+
{"type":"Name","value":"r"},
235+
{"type":"Punctuation","value":"];"},
236+
{"type":"Text","value":"\n"}
237+
]

0 commit comments

Comments
 (0)