您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

152 行
4.7 KiB

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