ampl.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.ampl
  4. ~~~~~~~~~~~~~~~~~~~~
  5. Lexers for the AMPL language.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. from pygments.lexer import RegexLexer, bygroups, using, this, words
  10. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  11. Number, Punctuation
  12. __all__ = ['AmplLexer']
  13. class AmplLexer(RegexLexer):
  14. """
  15. For `AMPL <http://ampl.com/>`_ source code.
  16. .. versionadded:: 2.2
  17. """
  18. name = 'Ampl'
  19. aliases = ['ampl']
  20. filenames = ['*.run']
  21. tokens = {
  22. 'root': [
  23. (r'\n', Text),
  24. (r'\s+', Text.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, Text, Name.Variable)),
  50. (r'(param)(\s*)(:)(\s*)(\w+)(\s*)(:)(\s*)((\w|\s)+)',
  51. bygroups(Keyword.Declaration, Text, Punctuation, Text,
  52. Name.Variable, Text, Punctuation, Text, Name.Variable)),
  53. (r'(let|fix|unfix)(\s*)((?:\{.*\})?)(\s*)(\w+)',
  54. bygroups(Keyword.Declaration, Text, using(this), Text, Name.Variable)),
  55. (words((
  56. 'abs', 'acos', 'acosh', 'alias', 'asin', 'asinh', 'atan', 'atan2',
  57. 'atanh', 'ceil', 'ctime', 'cos', 'exp', 'floor', 'log', 'log10',
  58. 'max', 'min', 'precision', 'round', 'sin', 'sinh', 'sqrt', 'tan',
  59. 'tanh', 'time', 'trunc', 'Beta', 'Cauchy', 'Exponential', 'Gamma',
  60. 'Irand224', 'Normal', 'Normal01', 'Poisson', 'Uniform', 'Uniform01',
  61. 'num', 'num0', 'ichar', 'char', 'length', 'substr', 'sprintf',
  62. 'match', 'sub', 'gsub', 'print', 'printf', 'next', 'nextw', 'prev',
  63. 'prevw', 'first', 'last', 'ord', 'ord0', 'card', 'arity',
  64. 'indexarity'), prefix=r'\b', suffix=r'\b'), Name.Builtin),
  65. (r'(\+|\-|\*|/|\*\*|=|<=|>=|==|\||\^|<|>|\!|\.\.|:=|\&|\!=|<<|>>)',
  66. Operator),
  67. (words((
  68. 'or', 'exists', 'forall', 'and', 'in', 'not', 'within', 'union',
  69. 'diff', 'difference', 'symdiff', 'inter', 'intersect',
  70. 'intersection', 'cross', 'setof', 'by', 'less', 'sum', 'prod',
  71. 'product', 'div', 'mod'), suffix=r'\b'),
  72. Keyword.Reserved), # Operator.Name but not enough emphasized with that
  73. (r'(\d+\.(?!\.)\d*|\.(?!.)\d+)([eE][+-]?\d+)?', Number.Float),
  74. (r'\d+([eE][+-]?\d+)?', Number.Integer),
  75. (r'[+-]?Infinity', Number.Integer),
  76. (r'(\w+|(\.(?!\.)))', Text)
  77. ]
  78. }