forth.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.forth
  4. ~~~~~~~~~~~~~~~~~~~~~
  5. Lexer for the Forth language.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. import re
  10. from pygments.lexer import RegexLexer, include, bygroups
  11. from pygments.token import Error, Punctuation, Literal, Token, \
  12. Text, Comment, Operator, Keyword, Name, String, Number, Generic
  13. __all__ = ['ForthLexer']
  14. class ForthLexer(RegexLexer):
  15. """
  16. Lexer for Forth files.
  17. .. versionadded:: 2.2
  18. """
  19. name = 'Forth'
  20. aliases = ['forth']
  21. filenames = ['*.frt', '*.fs']
  22. mimetypes = ['application/x-forth']
  23. delimiter = r'\s'
  24. delimiter_end = r'(?=[%s])' % delimiter
  25. valid_name_chars = r'[^%s]' % delimiter
  26. valid_name = r"%s+%s" % (valid_name_chars, delimiter_end)
  27. flags = re.IGNORECASE | re.MULTILINE
  28. tokens = {
  29. 'root': [
  30. (r'\s+', Text),
  31. # All comment types
  32. (r'\\.*?\n', Comment.Single),
  33. (r'\([\s].*?\)', Comment.Single),
  34. # defining words. The next word is a new command name
  35. (r'(:|variable|constant|value|buffer:)(\s+)',
  36. bygroups(Keyword.Namespace, Text), 'worddef'),
  37. # strings are rather simple
  38. (r'([.sc]")(\s+?)', bygroups(String, Text), 'stringdef'),
  39. # keywords from the various wordsets
  40. # *** Wordset BLOCK
  41. (r'(blk|block|buffer|evaluate|flush|load|save-buffers|update|'
  42. # *** Wordset BLOCK-EXT
  43. r'empty-buffers|list|refill|scr|thru|'
  44. # *** Wordset CORE
  45. r'\#s|\*\/mod|\+loop|\/mod|0<|0=|1\+|1-|2!|'
  46. r'2\*|2\/|2@|2drop|2dup|2over|2swap|>body|'
  47. r'>in|>number|>r|\?dup|abort|abort\"|abs|'
  48. r'accept|align|aligned|allot|and|base|begin|'
  49. r'bl|c!|c,|c@|cell\+|cells|char|char\+|'
  50. r'chars|constant|count|cr|create|decimal|'
  51. r'depth|do|does>|drop|dup|else|emit|environment\?|'
  52. r'evaluate|execute|exit|fill|find|fm\/mod|'
  53. r'here|hold|i|if|immediate|invert|j|key|'
  54. r'leave|literal|loop|lshift|m\*|max|min|'
  55. r'mod|move|negate|or|over|postpone|quit|'
  56. r'r>|r@|recurse|repeat|rot|rshift|s\"|s>d|'
  57. r'sign|sm\/rem|source|space|spaces|state|swap|'
  58. r'then|type|u\.|u\<|um\*|um\/mod|unloop|until|'
  59. r'variable|while|word|xor|\[char\]|\[\'\]|'
  60. r'@|!|\#|<\#|\#>|:|;|\+|-|\*|\/|,|<|>|\|1\+|1-|\.|'
  61. # *** Wordset CORE-EXT
  62. r'\.r|0<>|'
  63. r'0>|2>r|2r>|2r@|:noname|\?do|again|c\"|'
  64. r'case|compile,|endcase|endof|erase|false|'
  65. r'hex|marker|nip|of|pad|parse|pick|refill|'
  66. r'restore-input|roll|save-input|source-id|to|'
  67. r'true|tuck|u\.r|u>|unused|value|within|'
  68. r'\[compile\]|'
  69. # *** Wordset CORE-EXT-obsolescent
  70. r'\#tib|convert|expect|query|span|'
  71. r'tib|'
  72. # *** Wordset DOUBLE
  73. r'2constant|2literal|2variable|d\+|d-|'
  74. r'd\.|d\.r|d0<|d0=|d2\*|d2\/|d<|d=|d>s|'
  75. r'dabs|dmax|dmin|dnegate|m\*\/|m\+|'
  76. # *** Wordset DOUBLE-EXT
  77. r'2rot|du<|'
  78. # *** Wordset EXCEPTION
  79. r'catch|throw|'
  80. # *** Wordset EXCEPTION-EXT
  81. r'abort|abort\"|'
  82. # *** Wordset FACILITY
  83. r'at-xy|key\?|page|'
  84. # *** Wordset FACILITY-EXT
  85. r'ekey|ekey>char|ekey\?|emit\?|ms|time&date|'
  86. # *** Wordset FILE
  87. r'BIN|CLOSE-FILE|CREATE-FILE|DELETE-FILE|FILE-POSITION|'
  88. r'FILE-SIZE|INCLUDE-FILE|INCLUDED|OPEN-FILE|R\/O|'
  89. r'R\/W|READ-FILE|READ-LINE|REPOSITION-FILE|RESIZE-FILE|'
  90. r'S\"|SOURCE-ID|W/O|WRITE-FILE|WRITE-LINE|'
  91. # *** Wordset FILE-EXT
  92. r'FILE-STATUS|FLUSH-FILE|REFILL|RENAME-FILE|'
  93. # *** Wordset FLOAT
  94. r'>float|d>f|'
  95. r'f!|f\*|f\+|f-|f\/|f0<|f0=|f<|f>d|f@|'
  96. r'falign|faligned|fconstant|fdepth|fdrop|fdup|'
  97. r'fliteral|float\+|floats|floor|fmax|fmin|'
  98. r'fnegate|fover|frot|fround|fswap|fvariable|'
  99. r'represent|'
  100. # *** Wordset FLOAT-EXT
  101. r'df!|df@|dfalign|dfaligned|dfloat\+|'
  102. r'dfloats|f\*\*|f\.|fabs|facos|facosh|falog|'
  103. r'fasin|fasinh|fatan|fatan2|fatanh|fcos|fcosh|'
  104. r'fe\.|fexp|fexpm1|fln|flnp1|flog|fs\.|fsin|'
  105. r'fsincos|fsinh|fsqrt|ftan|ftanh|f~|precision|'
  106. r'set-precision|sf!|sf@|sfalign|sfaligned|sfloat\+|'
  107. r'sfloats|'
  108. # *** Wordset LOCAL
  109. r'\(local\)|to|'
  110. # *** Wordset LOCAL-EXT
  111. r'locals\||'
  112. # *** Wordset MEMORY
  113. r'allocate|free|resize|'
  114. # *** Wordset SEARCH
  115. r'definitions|find|forth-wordlist|get-current|'
  116. r'get-order|search-wordlist|set-current|set-order|'
  117. r'wordlist|'
  118. # *** Wordset SEARCH-EXT
  119. r'also|forth|only|order|previous|'
  120. # *** Wordset STRING
  121. r'-trailing|\/string|blank|cmove|cmove>|compare|'
  122. r'search|sliteral|'
  123. # *** Wordset TOOLS
  124. r'.s|dump|see|words|'
  125. # *** Wordset TOOLS-EXT
  126. r';code|'
  127. r'ahead|assembler|bye|code|cs-pick|cs-roll|'
  128. r'editor|state|\[else\]|\[if\]|\[then\]|'
  129. # *** Wordset TOOLS-EXT-obsolescent
  130. r'forget|'
  131. # Forth 2012
  132. r'defer|defer@|defer!|action-of|begin-structure|field:|buffer:|'
  133. r'parse-name|buffer:|traverse-wordlist|n>r|nr>|2value|fvalue|'
  134. r'name>interpret|name>compile|name>string|'
  135. r'cfield:|end-structure)'+delimiter, Keyword),
  136. # Numbers
  137. (r'(\$[0-9A-F]+)', Number.Hex),
  138. (r'(\#|%|&|\-|\+)?[0-9]+', Number.Integer),
  139. (r'(\#|%|&|\-|\+)?[0-9.]+', Keyword.Type),
  140. # amforth specific
  141. (r'(@i|!i|@e|!e|pause|noop|turnkey|sleep|'
  142. r'itype|icompare|sp@|sp!|rp@|rp!|up@|up!|'
  143. r'>a|a>|a@|a!|a@+|a@-|>b|b>|b@|b!|b@+|b@-|'
  144. r'find-name|1ms|'
  145. r'sp0|rp0|\(evaluate\)|int-trap|int!)' + delimiter,
  146. Name.Constant),
  147. # a proposal
  148. (r'(do-recognizer|r:fail|recognizer:|get-recognizers|'
  149. r'set-recognizers|r:float|r>comp|r>int|r>post|'
  150. r'r:name|r:word|r:dnum|r:num|recognizer|forth-recognizer|'
  151. r'rec:num|rec:float|rec:word)' + delimiter, Name.Decorator),
  152. # defining words. The next word is a new command name
  153. (r'(Evalue|Rvalue|Uvalue|Edefer|Rdefer|Udefer)(\s+)',
  154. bygroups(Keyword.Namespace, Text), 'worddef'),
  155. (valid_name, Name.Function), # Anything else is executed
  156. ],
  157. 'worddef': [
  158. (r'\S+', Name.Class, '#pop'),
  159. ],
  160. 'stringdef': [
  161. (r'[^"]+', String, '#pop'),
  162. ],
  163. }