forth.py 7.0 KB

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