Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
fantom.py252 linesDownload Raw Back to lexers
1"""2    pygments.lexers.fantom3    ~~~~~~~~~~~~~~~~~~~~~~4 5    Lexer for the Fantom language.6 7    :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.8    :license: BSD, see LICENSE for details.9"""10 11from string import Template12 13from pygments.lexer import RegexLexer, include, bygroups, using, \14    this, default, words15from pygments.token import Text, Comment, Operator, Keyword, Name, String, \16    Number, Punctuation, Literal, Whitespace17 18__all__ = ['FantomLexer']19 20 21class FantomLexer(RegexLexer):22    """23    For Fantom source code.24    """25    name = 'Fantom'26    aliases = ['fan']27    filenames = ['*.fan']28    mimetypes = ['application/x-fantom']29    url = 'https://www.fantom.org'30    version_added = '1.5'31 32    # often used regexes33    def s(str):34        return Template(str).substitute(35            dict(36                pod=r'[\"\w\.]+',37                eos=r'\n|;',38                id=r'[a-zA-Z_]\w*',39                # all chars which can be part of type definition. Starts with40                # either letter, or [ (maps), or | (funcs)41                type=r'(?:\[|[a-zA-Z_]|\|)[:\w\[\]|\->?]*?',42            )43        )44 45    tokens = {46        'comments': [47            (r'(?s)/\*.*?\*/', Comment.Multiline),           # Multiline48            (r'//.*?$', Comment.Single),                    # Single line49            # TODO: highlight references in fandocs50            (r'\*\*.*?$', Comment.Special),                 # Fandoc51            (r'#.*$', Comment.Single)                       # Shell-style52        ],53        'literals': [54            (r'\b-?[\d_]+(ns|ms|sec|min|hr|day)', Number),   # Duration55            (r'\b-?[\d_]*\.[\d_]+(ns|ms|sec|min|hr|day)', Number),  # Duration with dot56            (r'\b-?(\d+)?\.\d+(f|F|d|D)?', Number.Float),    # Float/Decimal57            (r'\b-?0x[0-9a-fA-F_]+', Number.Hex),            # Hex58            (r'\b-?[\d_]+', Number.Integer),                 # Int59            (r"'\\.'|'[^\\]'|'\\u[0-9a-f]{4}'", String.Char),  # Char60            (r'"', Punctuation, 'insideStr'),                # Opening quote61            (r'`', Punctuation, 'insideUri'),                # Opening accent62            (r'\b(true|false|null)\b', Keyword.Constant),    # Bool & null63            (r'(?:(\w+)(::))?(\w+)(<\|)(.*?)(\|>)',          # DSL64             bygroups(Name.Namespace, Punctuation, Name.Class,65                      Punctuation, String, Punctuation)),66            (r'(?:(\w+)(::))?(\w+)?(#)(\w+)?',               # Type/slot literal67             bygroups(Name.Namespace, Punctuation, Name.Class,68                      Punctuation, Name.Function)),69            (r'\[,\]', Literal),                             # Empty list70            (s(r'($type)(\[,\])'),                           # Typed empty list71             bygroups(using(this, state='inType'), Literal)),72            (r'\[:\]', Literal),                             # Empty Map73            (s(r'($type)(\[:\])'),74             bygroups(using(this, state='inType'), Literal)),75        ],76        'insideStr': [77            (r'\\\\', String.Escape),                        # Escaped backslash78            (r'\\"', String.Escape),                         # Escaped "79            (r'\\`', String.Escape),                         # Escaped `80            (r'\$\w+', String.Interpol),                     # Subst var81            (r'\$\{.*?\}', String.Interpol),                 # Subst expr82            (r'"', Punctuation, '#pop'),                     # Closing quot83            (r'.', String)                                   # String content84        ],85        'insideUri': [  # TODO: remove copy/paste str/uri86            (r'\\\\', String.Escape),                        # Escaped backslash87            (r'\\"', String.Escape),                         # Escaped "88            (r'\\`', String.Escape),                         # Escaped `89            (r'\$\w+', String.Interpol),                     # Subst var90            (r'\$\{.*?\}', String.Interpol),                 # Subst expr91            (r'`', Punctuation, '#pop'),                     # Closing tick92            (r'.', String.Backtick)                          # URI content93        ],94        'protectionKeywords': [95            (r'\b(public|protected|private|internal)\b', Keyword),96        ],97        'typeKeywords': [98            (r'\b(abstract|final|const|native|facet|enum)\b', Keyword),99        ],100        'methodKeywords': [101            (r'\b(abstract|native|once|override|static|virtual|final)\b',102             Keyword),103        ],104        'fieldKeywords': [105            (r'\b(abstract|const|final|native|override|static|virtual|'106             r'readonly)\b', Keyword)107        ],108        'otherKeywords': [109            (words((110                'try', 'catch', 'throw', 'finally', 'for', 'if', 'else', 'while',111                'as', 'is', 'isnot', 'switch', 'case', 'default', 'continue',112                'break', 'do', 'return', 'get', 'set'), prefix=r'\b', suffix=r'\b'),113             Keyword),114            (r'\b(it|this|super)\b', Name.Builtin.Pseudo),115        ],116        'operators': [117            (r'\+\+|\-\-|\+|\-|\*|/|\|\||&&|<=>|<=|<|>=|>|=|!|\[|\]', Operator)118        ],119        'inType': [120            (r'[\[\]|\->:?]', Punctuation),121            (s(r'$id'), Name.Class),122            default('#pop'),123 124        ],125        'root': [126            include('comments'),127            include('protectionKeywords'),128            include('typeKeywords'),129            include('methodKeywords'),130            include('fieldKeywords'),131            include('literals'),132            include('otherKeywords'),133            include('operators'),134            (r'using\b', Keyword.Namespace, 'using'),         # Using stmt135            (r'@\w+', Name.Decorator, 'facet'),               # Symbol136            (r'(class|mixin)(\s+)(\w+)', bygroups(Keyword, Whitespace, Name.Class),137             'inheritance'),                                  # Inheritance list138 139            # Type var := val140            (s(r'($type)([ \t]+)($id)(\s*)(:=)'),141             bygroups(using(this, state='inType'), Whitespace,142                      Name.Variable, Whitespace, Operator)),143 144            # var := val145            (s(r'($id)(\s*)(:=)'),146             bygroups(Name.Variable, Whitespace, Operator)),147 148            # .someId( or ->someId( ###149            (s(r'(\.|(?:\->))($id)(\s*)(\()'),150             bygroups(Operator, Name.Function, Whitespace, Punctuation),151             'insideParen'),152 153            # .someId  or ->someId154            (s(r'(\.|(?:\->))($id)'),155             bygroups(Operator, Name.Function)),156 157            # new makeXXX (158            (r'(new)(\s+)(make\w*)(\s*)(\()',159             bygroups(Keyword, Whitespace, Name.Function, Whitespace, Punctuation),160             'insideMethodDeclArgs'),161 162            # Type name (163            (s(r'($type)([ \t]+)'  # Return type and whitespace164               r'($id)(\s*)(\()'),  # method name + open brace165             bygroups(using(this, state='inType'), Whitespace,166                      Name.Function, Whitespace, Punctuation),167             'insideMethodDeclArgs'),168 169            # ArgType argName,170            (s(r'($type)(\s+)($id)(\s*)(,)'),171             bygroups(using(this, state='inType'), Whitespace, Name.Variable,172                      Whitespace, Punctuation)),173 174            # ArgType argName)175            # Covered in 'insideParen' state176 177            # ArgType argName -> ArgType|178            (s(r'($type)(\s+)($id)(\s*)(\->)(\s*)($type)(\|)'),179             bygroups(using(this, state='inType'), Whitespace, Name.Variable,180                      Whitespace, Punctuation, Whitespace, using(this, state='inType'),181                      Punctuation)),182 183            # ArgType argName|184            (s(r'($type)(\s+)($id)(\s*)(\|)'),185             bygroups(using(this, state='inType'), Whitespace, Name.Variable,186                      Whitespace, Punctuation)),187 188            # Type var189            (s(r'($type)([ \t]+)($id)'),190             bygroups(using(this, state='inType'), Whitespace,191                      Name.Variable)),192 193            (r'\(', Punctuation, 'insideParen'),194            (r'\{', Punctuation, 'insideBrace'),195            (r'\s+', Whitespace),196            (r'.', Text)197        ],198        'insideParen': [199            (r'\)', Punctuation, '#pop'),200            include('root'),201        ],202        'insideMethodDeclArgs': [203            (r'\)', Punctuation, '#pop'),204            (s(r'($type)(\s+)($id)(\s*)(\))'),205             bygroups(using(this, state='inType'), Whitespace, Name.Variable,206                      Whitespace, Punctuation), '#pop'),207            include('root'),208        ],209        'insideBrace': [210            (r'\}', Punctuation, '#pop'),211            include('root'),212        ],213        'inheritance': [214            (r'\s+', Whitespace),                                      # Whitespace215            (r':|,', Punctuation),216            (r'(?:(\w+)(::))?(\w+)',217             bygroups(Name.Namespace, Punctuation, Name.Class)),218            (r'\{', Punctuation, '#pop')219        ],220        'using': [221            (r'[ \t]+', Whitespace),  # consume whitespaces222            (r'(\[)(\w+)(\])',223             bygroups(Punctuation, Comment.Special, Punctuation)),  # ffi224            (r'(\")?([\w.]+)(\")?',225             bygroups(Punctuation, Name.Namespace, Punctuation)),  # podname226            (r'::', Punctuation, 'usingClass'),227            default('#pop')228        ],229        'usingClass': [230            (r'[ \t]+', Whitespace),  # consume whitespaces231            (r'(as)(\s+)(\w+)',232             bygroups(Keyword.Declaration, Whitespace, Name.Class), '#pop:2'),233            (r'[\w$]+', Name.Class),234            default('#pop:2')  # jump out to root state235        ],236        'facet': [237            (r'\s+', Whitespace),238            (r'\{', Punctuation, 'facetFields'),239            default('#pop')240        ],241        'facetFields': [242            include('comments'),243            include('literals'),244            include('operators'),245            (r'\s+', Whitespace),246            (r'(\s*)(\w+)(\s*)(=)', bygroups(Whitespace, Name, Whitespace, Operator)),247            (r'\}', Punctuation, '#pop'),248            (r'\s+', Whitespace),249            (r'.', Text)250        ],251    }252 
codekingpro/portable-devtools · Team Ai