Team Ai
Datasetpublic

codekingpro/portable-devtools

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