ampl.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """
  2. pygments.lexers.ampl
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Lexers for the AMPL language.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, bygroups, using, this, words
  9. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  10. Number, Punctuation
  11. __all__ = ['AmplLexer']
  12. class AmplLexer(RegexLexer):
  13. """
  14. For `AMPL <http://ampl.com/>`_ source code.
  15. .. versionadded:: 2.2
  16. """
  17. name = 'Ampl'
  18. aliases = ['ampl']
  19. filenames = ['*.run']
  20. tokens = {
  21. 'root': [
  22. (r'\n', Text),
  23. (r'\s+', Text.Whitespace),
  24. (r'#.*?\n', Comment.Single),
  25. (r'/[*](.|\n)*?[*]/', Comment.Multiline),
  26. (words((
  27. 'call', 'cd', 'close', 'commands', 'data', 'delete', 'display',
  28. 'drop', 'end', 'environ', 'exit', 'expand', 'include', 'load',
  29. 'model', 'objective', 'option', 'problem', 'purge', 'quit',
  30. 'redeclare', 'reload', 'remove', 'reset', 'restore', 'shell',
  31. 'show', 'solexpand', 'solution', 'solve', 'update', 'unload',
  32. 'xref', 'coeff', 'coef', 'cover', 'obj', 'interval', 'default',
  33. 'from', 'to', 'to_come', 'net_in', 'net_out', 'dimen',
  34. 'dimension', 'check', 'complements', 'write', 'function',
  35. 'pipe', 'format', 'if', 'then', 'else', 'in', 'while', 'repeat',
  36. 'for'), suffix=r'\b'), Keyword.Reserved),
  37. (r'(integer|binary|symbolic|ordered|circular|reversed|INOUT|IN|OUT|LOCAL)',
  38. Keyword.Type),
  39. (r'\".*?\"', String.Double),
  40. (r'\'.*?\'', String.Single),
  41. (r'[()\[\]{},;:]+', Punctuation),
  42. (r'\b(\w+)(\.)(astatus|init0|init|lb0|lb1|lb2|lb|lrc|'
  43. r'lslack|rc|relax|slack|sstatus|status|ub0|ub1|ub2|'
  44. r'ub|urc|uslack|val)',
  45. bygroups(Name.Variable, Punctuation, Keyword.Reserved)),
  46. (r'(set|param|var|arc|minimize|maximize|subject to|s\.t\.|subj to|'
  47. r'node|table|suffix|read table|write table)(\s+)(\w+)',
  48. bygroups(Keyword.Declaration, Text, Name.Variable)),
  49. (r'(param)(\s*)(:)(\s*)(\w+)(\s*)(:)(\s*)((\w|\s)+)',
  50. bygroups(Keyword.Declaration, Text, Punctuation, Text,
  51. Name.Variable, Text, Punctuation, Text, Name.Variable)),
  52. (r'(let|fix|unfix)(\s*)((?:\{.*\})?)(\s*)(\w+)',
  53. bygroups(Keyword.Declaration, Text, using(this), Text, Name.Variable)),
  54. (words((
  55. 'abs', 'acos', 'acosh', 'alias', 'asin', 'asinh', 'atan', 'atan2',
  56. 'atanh', 'ceil', 'ctime', 'cos', 'exp', 'floor', 'log', 'log10',
  57. 'max', 'min', 'precision', 'round', 'sin', 'sinh', 'sqrt', 'tan',
  58. 'tanh', 'time', 'trunc', 'Beta', 'Cauchy', 'Exponential', 'Gamma',
  59. 'Irand224', 'Normal', 'Normal01', 'Poisson', 'Uniform', 'Uniform01',
  60. 'num', 'num0', 'ichar', 'char', 'length', 'substr', 'sprintf',
  61. 'match', 'sub', 'gsub', 'print', 'printf', 'next', 'nextw', 'prev',
  62. 'prevw', 'first', 'last', 'ord', 'ord0', 'card', 'arity',
  63. 'indexarity'), prefix=r'\b', suffix=r'\b'), Name.Builtin),
  64. (r'(\+|\-|\*|/|\*\*|=|<=|>=|==|\||\^|<|>|\!|\.\.|:=|\&|\!=|<<|>>)',
  65. Operator),
  66. (words((
  67. 'or', 'exists', 'forall', 'and', 'in', 'not', 'within', 'union',
  68. 'diff', 'difference', 'symdiff', 'inter', 'intersect',
  69. 'intersection', 'cross', 'setof', 'by', 'less', 'sum', 'prod',
  70. 'product', 'div', 'mod'), suffix=r'\b'),
  71. Keyword.Reserved), # Operator.Name but not enough emphasized with that
  72. (r'(\d+\.(?!\.)\d*|\.(?!.)\d+)([eE][+-]?\d+)?', Number.Float),
  73. (r'\d+([eE][+-]?\d+)?', Number.Integer),
  74. (r'[+-]?Infinity', Number.Integer),
  75. (r'(\w+|(\.(?!\.)))', Text)
  76. ]
  77. }