ampl.py 4.1 KB

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