forth.py 7.0 KB

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