j.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. """
  2. pygments.lexers.j
  3. ~~~~~~~~~~~~~~~~~
  4. Lexer for the J programming 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, words, include
  9. from pygments.token import Comment, Keyword, Name, Number, Operator, Punctuation, \
  10. String, Text
  11. __all__ = ['JLexer']
  12. class JLexer(RegexLexer):
  13. """
  14. For `J <http://jsoftware.com/>`_ source code.
  15. .. versionadded:: 2.1
  16. """
  17. name = 'J'
  18. aliases = ['j']
  19. filenames = ['*.ijs']
  20. mimetypes = ['text/x-j']
  21. validName = r'\b[a-zA-Z]\w*'
  22. tokens = {
  23. 'root': [
  24. # Shebang script
  25. (r'#!.*$', Comment.Preproc),
  26. # Comments
  27. (r'NB\..*', Comment.Single),
  28. (r'\n+\s*Note', Comment.Multiline, 'comment'),
  29. (r'\s*Note.*', Comment.Single),
  30. # Whitespace
  31. (r'\s+', Text),
  32. # Strings
  33. (r"'", String, 'singlequote'),
  34. # Definitions
  35. (r'0\s+:\s*0|noun\s+define\s*$', Name.Entity, 'nounDefinition'),
  36. (r'(([1-4]|13)\s+:\s*0|(adverb|conjunction|dyad|monad|verb)\s+define)\b',
  37. Name.Function, 'explicitDefinition'),
  38. # Flow Control
  39. (words(('for_', 'goto_', 'label_'), suffix=validName+r'\.'), Name.Label),
  40. (words((
  41. 'assert', 'break', 'case', 'catch', 'catchd',
  42. 'catcht', 'continue', 'do', 'else', 'elseif',
  43. 'end', 'fcase', 'for', 'if', 'return',
  44. 'select', 'throw', 'try', 'while', 'whilst',
  45. ), suffix=r'\.'), Name.Label),
  46. # Variable Names
  47. (validName, Name.Variable),
  48. # Standard Library
  49. (words((
  50. 'ARGV', 'CR', 'CRLF', 'DEL', 'Debug',
  51. 'EAV', 'EMPTY', 'FF', 'JVERSION', 'LF',
  52. 'LF2', 'Note', 'TAB', 'alpha17', 'alpha27',
  53. 'apply', 'bind', 'boxopen', 'boxxopen', 'bx',
  54. 'clear', 'cutLF', 'cutopen', 'datatype', 'def',
  55. 'dfh', 'drop', 'each', 'echo', 'empty',
  56. 'erase', 'every', 'evtloop', 'exit', 'expand',
  57. 'fetch', 'file2url', 'fixdotdot', 'fliprgb', 'getargs',
  58. 'getenv', 'hfd', 'inv', 'inverse', 'iospath',
  59. 'isatty', 'isutf8', 'items', 'leaf', 'list',
  60. 'nameclass', 'namelist', 'names', 'nc',
  61. 'nl', 'on', 'pick', 'rows',
  62. 'script', 'scriptd', 'sign', 'sminfo', 'smoutput',
  63. 'sort', 'split', 'stderr', 'stdin', 'stdout',
  64. 'table', 'take', 'timespacex', 'timex', 'tmoutput',
  65. 'toCRLF', 'toHOST', 'toJ', 'tolower', 'toupper',
  66. 'type', 'ucp', 'ucpcount', 'usleep', 'utf8',
  67. 'uucp',
  68. )), Name.Function),
  69. # Copula
  70. (r'=[.:]', Operator),
  71. # Builtins
  72. (r'[-=+*#$%@!~`^&";:.,<>{}\[\]\\|/?]', Operator),
  73. # Short Keywords
  74. (r'[abCdDeEfHiIjLMoprtT]\.', Keyword.Reserved),
  75. (r'[aDiLpqsStux]\:', Keyword.Reserved),
  76. (r'(_[0-9])\:', Keyword.Constant),
  77. # Parens
  78. (r'\(', Punctuation, 'parentheses'),
  79. # Numbers
  80. include('numbers'),
  81. ],
  82. 'comment': [
  83. (r'[^)]', Comment.Multiline),
  84. (r'^\)', Comment.Multiline, '#pop'),
  85. (r'[)]', Comment.Multiline),
  86. ],
  87. 'explicitDefinition': [
  88. (r'\b[nmuvxy]\b', Name.Decorator),
  89. include('root'),
  90. (r'[^)]', Name),
  91. (r'^\)', Name.Label, '#pop'),
  92. (r'[)]', Name),
  93. ],
  94. 'numbers': [
  95. (r'\b_{1,2}\b', Number),
  96. (r'_?\d+(\.\d+)?(\s*[ejr]\s*)_?\d+(\.?=\d+)?', Number),
  97. (r'_?\d+\.(?=\d+)', Number.Float),
  98. (r'_?\d+x', Number.Integer.Long),
  99. (r'_?\d+', Number.Integer),
  100. ],
  101. 'nounDefinition': [
  102. (r'[^)]', String),
  103. (r'^\)', Name.Label, '#pop'),
  104. (r'[)]', String),
  105. ],
  106. 'parentheses': [
  107. (r'\)', Punctuation, '#pop'),
  108. # include('nounDefinition'),
  109. include('explicitDefinition'),
  110. include('root'),
  111. ],
  112. 'singlequote': [
  113. (r"[^']", String),
  114. (r"''", String),
  115. (r"'", String, '#pop'),
  116. ],
  117. }