j.py 4.4 KB

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