|
""" |
|
pygments.lexers.fantom |
|
~~~~~~~~~~~~~~~~~~~~~~ |
|
|
|
Lexer for the Fantom language. |
|
|
|
:copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS. |
|
:license: BSD, see LICENSE for details. |
|
""" |
|
|
|
from string import Template |
|
|
|
from pygments.lexer import RegexLexer, include, bygroups, using, \ |
|
this, default, words |
|
from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
|
Number, Punctuation, Literal, Whitespace |
|
|
|
__all__ = ['FantomLexer'] |
|
|
|
|
|
class FantomLexer(RegexLexer): |
|
""" |
|
For Fantom source code. |
|
""" |
|
name = 'Fantom' |
|
aliases = ['fan'] |
|
filenames = ['*.fan'] |
|
mimetypes = ['application/x-fantom'] |
|
url = 'https://www.fantom.org' |
|
version_added = '1.5' |
|
|
|
|
|
def s(str): |
|
return Template(str).substitute( |
|
dict( |
|
pod=r'[\"\w\.]+', |
|
eos=r'\n|;', |
|
id=r'[a-zA-Z_]\w*', |
|
|
|
|
|
type=r'(?:\[|[a-zA-Z_]|\|)[:\w\[\]|\->?]*?', |
|
) |
|
) |
|
|
|
tokens = { |
|
'comments': [ |
|
(r'(?s)/\*.*?\*/', Comment.Multiline), |
|
(r'//.*?$', Comment.Single), |
|
|
|
(r'\*\*.*?$', Comment.Special), |
|
(r'#.*$', Comment.Single) |
|
], |
|
'literals': [ |
|
(r'\b-?[\d_]+(ns|ms|sec|min|hr|day)', Number), |
|
(r'\b-?[\d_]*\.[\d_]+(ns|ms|sec|min|hr|day)', Number), |
|
(r'\b-?(\d+)?\.\d+(f|F|d|D)?', Number.Float), |
|
(r'\b-?0x[0-9a-fA-F_]+', Number.Hex), |
|
(r'\b-?[\d_]+', Number.Integer), |
|
(r"'\\.'|'[^\\]'|'\\u[0-9a-f]{4}'", String.Char), |
|
(r'"', Punctuation, 'insideStr'), |
|
(r'`', Punctuation, 'insideUri'), |
|
(r'\b(true|false|null)\b', Keyword.Constant), |
|
(r'(?:(\w+)(::))?(\w+)(<\|)(.*?)(\|>)', |
|
bygroups(Name.Namespace, Punctuation, Name.Class, |
|
Punctuation, String, Punctuation)), |
|
(r'(?:(\w+)(::))?(\w+)?(#)(\w+)?', |
|
bygroups(Name.Namespace, Punctuation, Name.Class, |
|
Punctuation, Name.Function)), |
|
(r'\[,\]', Literal), |
|
(s(r'($type)(\[,\])'), |
|
bygroups(using(this, state='inType'), Literal)), |
|
(r'\[:\]', Literal), |
|
(s(r'($type)(\[:\])'), |
|
bygroups(using(this, state='inType'), Literal)), |
|
], |
|
'insideStr': [ |
|
(r'\\\\', String.Escape), |
|
(r'\\"', String.Escape), |
|
(r'\\`', String.Escape), |
|
(r'\$\w+', String.Interpol), |
|
(r'\$\{.*?\}', String.Interpol), |
|
(r'"', Punctuation, '#pop'), |
|
(r'.', String) |
|
], |
|
'insideUri': [ |
|
(r'\\\\', String.Escape), |
|
(r'\\"', String.Escape), |
|
(r'\\`', String.Escape), |
|
(r'\$\w+', String.Interpol), |
|
(r'\$\{.*?\}', String.Interpol), |
|
(r'`', Punctuation, '#pop'), |
|
(r'.', String.Backtick) |
|
], |
|
'protectionKeywords': [ |
|
(r'\b(public|protected|private|internal)\b', Keyword), |
|
], |
|
'typeKeywords': [ |
|
(r'\b(abstract|final|const|native|facet|enum)\b', Keyword), |
|
], |
|
'methodKeywords': [ |
|
(r'\b(abstract|native|once|override|static|virtual|final)\b', |
|
Keyword), |
|
], |
|
'fieldKeywords': [ |
|
(r'\b(abstract|const|final|native|override|static|virtual|' |
|
r'readonly)\b', Keyword) |
|
], |
|
'otherKeywords': [ |
|
(words(( |
|
'try', 'catch', 'throw', 'finally', 'for', 'if', 'else', 'while', |
|
'as', 'is', 'isnot', 'switch', 'case', 'default', 'continue', |
|
'break', 'do', 'return', 'get', 'set'), prefix=r'\b', suffix=r'\b'), |
|
Keyword), |
|
(r'\b(it|this|super)\b', Name.Builtin.Pseudo), |
|
], |
|
'operators': [ |
|
(r'\+\+|\-\-|\+|\-|\*|/|\|\||&&|<=>|<=|<|>=|>|=|!|\[|\]', Operator) |
|
], |
|
'inType': [ |
|
(r'[\[\]|\->:?]', Punctuation), |
|
(s(r'$id'), Name.Class), |
|
default('#pop'), |
|
|
|
], |
|
'root': [ |
|
include('comments'), |
|
include('protectionKeywords'), |
|
include('typeKeywords'), |
|
include('methodKeywords'), |
|
include('fieldKeywords'), |
|
include('literals'), |
|
include('otherKeywords'), |
|
include('operators'), |
|
(r'using\b', Keyword.Namespace, 'using'), |
|
(r'@\w+', Name.Decorator, 'facet'), |
|
(r'(class|mixin)(\s+)(\w+)', bygroups(Keyword, Whitespace, Name.Class), |
|
'inheritance'), |
|
|
|
|
|
(s(r'($type)([ \t]+)($id)(\s*)(:=)'), |
|
bygroups(using(this, state='inType'), Whitespace, |
|
Name.Variable, Whitespace, Operator)), |
|
|
|
|
|
(s(r'($id)(\s*)(:=)'), |
|
bygroups(Name.Variable, Whitespace, Operator)), |
|
|
|
|
|
(s(r'(\.|(?:\->))($id)(\s*)(\()'), |
|
bygroups(Operator, Name.Function, Whitespace, Punctuation), |
|
'insideParen'), |
|
|
|
|
|
(s(r'(\.|(?:\->))($id)'), |
|
bygroups(Operator, Name.Function)), |
|
|
|
|
|
(r'(new)(\s+)(make\w*)(\s*)(\()', |
|
bygroups(Keyword, Whitespace, Name.Function, Whitespace, Punctuation), |
|
'insideMethodDeclArgs'), |
|
|
|
|
|
(s(r'($type)([ \t]+)' |
|
r'($id)(\s*)(\()'), |
|
bygroups(using(this, state='inType'), Whitespace, |
|
Name.Function, Whitespace, Punctuation), |
|
'insideMethodDeclArgs'), |
|
|
|
|
|
(s(r'($type)(\s+)($id)(\s*)(,)'), |
|
bygroups(using(this, state='inType'), Whitespace, Name.Variable, |
|
Whitespace, Punctuation)), |
|
|
|
|
|
|
|
|
|
|
|
(s(r'($type)(\s+)($id)(\s*)(\->)(\s*)($type)(\|)'), |
|
bygroups(using(this, state='inType'), Whitespace, Name.Variable, |
|
Whitespace, Punctuation, Whitespace, using(this, state='inType'), |
|
Punctuation)), |
|
|
|
|
|
(s(r'($type)(\s+)($id)(\s*)(\|)'), |
|
bygroups(using(this, state='inType'), Whitespace, Name.Variable, |
|
Whitespace, Punctuation)), |
|
|
|
|
|
(s(r'($type)([ \t]+)($id)'), |
|
bygroups(using(this, state='inType'), Whitespace, |
|
Name.Variable)), |
|
|
|
(r'\(', Punctuation, 'insideParen'), |
|
(r'\{', Punctuation, 'insideBrace'), |
|
(r'\s+', Whitespace), |
|
(r'.', Text) |
|
], |
|
'insideParen': [ |
|
(r'\)', Punctuation, '#pop'), |
|
include('root'), |
|
], |
|
'insideMethodDeclArgs': [ |
|
(r'\)', Punctuation, '#pop'), |
|
(s(r'($type)(\s+)($id)(\s*)(\))'), |
|
bygroups(using(this, state='inType'), Whitespace, Name.Variable, |
|
Whitespace, Punctuation), '#pop'), |
|
include('root'), |
|
], |
|
'insideBrace': [ |
|
(r'\}', Punctuation, '#pop'), |
|
include('root'), |
|
], |
|
'inheritance': [ |
|
(r'\s+', Whitespace), |
|
(r':|,', Punctuation), |
|
(r'(?:(\w+)(::))?(\w+)', |
|
bygroups(Name.Namespace, Punctuation, Name.Class)), |
|
(r'\{', Punctuation, '#pop') |
|
], |
|
'using': [ |
|
(r'[ \t]+', Whitespace), |
|
(r'(\[)(\w+)(\])', |
|
bygroups(Punctuation, Comment.Special, Punctuation)), |
|
(r'(\")?([\w.]+)(\")?', |
|
bygroups(Punctuation, Name.Namespace, Punctuation)), |
|
(r'::', Punctuation, 'usingClass'), |
|
default('#pop') |
|
], |
|
'usingClass': [ |
|
(r'[ \t]+', Whitespace), |
|
(r'(as)(\s+)(\w+)', |
|
bygroups(Keyword.Declaration, Whitespace, Name.Class), '#pop:2'), |
|
(r'[\w$]+', Name.Class), |
|
default('#pop:2') |
|
], |
|
'facet': [ |
|
(r'\s+', Whitespace), |
|
(r'\{', Punctuation, 'facetFields'), |
|
default('#pop') |
|
], |
|
'facetFields': [ |
|
include('comments'), |
|
include('literals'), |
|
include('operators'), |
|
(r'\s+', Whitespace), |
|
(r'(\s*)(\w+)(\s*)(=)', bygroups(Whitespace, Name, Whitespace, Operator)), |
|
(r'\}', Punctuation, '#pop'), |
|
(r'\s+', Whitespace), |
|
(r'.', Text) |
|
], |
|
} |
|
|