|
| 1 | +""" |
| 2 | + pygments.lexers.roc |
| 3 | + ~~~~~~~~~~~~~~~~~~~ |
| 4 | +
|
| 5 | + Lexer for the Roc programming language. |
| 6 | +
|
| 7 | + :copyright: Copyright 2006-present by the Pygments team, see AUTHORS. |
| 8 | + :license: BSD, see LICENSE for details. |
| 9 | +""" |
| 10 | + |
| 11 | +from pygments.lexer import RegexLexer, words, include |
| 12 | +from pygments.token import Comment, Keyword, Name, Number, Operator, \ |
| 13 | + Punctuation, String, Whitespace |
| 14 | + |
| 15 | +__all__ = ['RocLexer'] |
| 16 | + |
| 17 | + |
| 18 | +class RocLexer(RegexLexer): |
| 19 | + """ |
| 20 | + For Roc source code. |
| 21 | + """ |
| 22 | + |
| 23 | + name = 'Roc' |
| 24 | + url = 'https://www.roc-lang.org/' |
| 25 | + aliases = ['roc'] |
| 26 | + filenames = ['*.roc'] |
| 27 | + mimetypes = ['text/x-roc'] |
| 28 | + version_added = '2.22' |
| 29 | + |
| 30 | + keywords = words(( |
| 31 | + 'app', 'as', 'break', 'crash', 'dbg', 'else', 'expect', 'for', |
| 32 | + 'if', 'import', 'in', 'match', 'platform', 'return', 'var', |
| 33 | + 'where', 'while', |
| 34 | + ), suffix=r'\b') |
| 35 | + |
| 36 | + # Word operators. |
| 37 | + word_operators = words(('and', 'or'), suffix=r'\b') |
| 38 | + |
| 39 | + # Multi-character operators, longest first. |
| 40 | + operators = words(( |
| 41 | + '...', '..=', '..', ':=', '::', '=>', '->', '|>', '==', '!=', |
| 42 | + '<=', '>=', '??', '?:', '//', '.?', |
| 43 | + )) |
| 44 | + |
| 45 | + tokens = { |
| 46 | + 'root': [ |
| 47 | + # Comments |
| 48 | + (r'#.*?$', Comment.Single), |
| 49 | + # Block strings: a line whose content starts with `\\` |
| 50 | + (r'\\\\', String, 'blockstring'), |
| 51 | + include('expr'), |
| 52 | + ], |
| 53 | + # The rules of a plain expression; shared between the root state |
| 54 | + # and `${...}` interpolations. |
| 55 | + 'expr': [ |
| 56 | + (r'\s+', Whitespace), |
| 57 | + # Strings |
| 58 | + (r'"', String, 'string'), |
| 59 | + # Keywords |
| 60 | + (keywords, Keyword.Reserved), |
| 61 | + (word_operators, Operator.Word), |
| 62 | + # Numbers with an explicit type suffix, e.g. ``5.U8`` |
| 63 | + (r'\d+\.(?:U8|I8|U16|I16|U32|I32|U64|I64|U128|I128|F32|F64|' |
| 64 | + r'Dec)\b', Number.Integer), |
| 65 | + (r'0[xX][0-9a-fA-F]+', Number.Hex), |
| 66 | + (r'0[oO][0-7]+', Number.Oct), |
| 67 | + (r'0[bB][01]+', Number.Bin), |
| 68 | + (r'\d+\.\d+', Number.Float), |
| 69 | + (r'\d+', Number.Integer), |
| 70 | + # ``var`` names are prefixed with ``$`` |
| 71 | + (r'\$[a-z][a-zA-Z0-9_]*', Name.Variable), |
| 72 | + # A lowercase name starting a line is a definition |
| 73 | + (r'^[a-z_][a-zA-Z0-9_]*', Name.Function), |
| 74 | + # Tags, nominal types and module names are uppercase |
| 75 | + (r'[A-Z][a-zA-Z0-9_]*', Keyword.Type), |
| 76 | + (r'[a-z_][a-zA-Z0-9_]*', Name), |
| 77 | + # Operators |
| 78 | + (operators, Operator), |
| 79 | + (r'[+\-*/%<>!?=|&.]', Operator), |
| 80 | + # Punctuation |
| 81 | + (r'[,()\[\]{}:]', Punctuation), |
| 82 | + ], |
| 83 | + # Interpolation inside a string or block string: the opening |
| 84 | + # ``${`` was already consumed. A ``}`` ends the interpolation, |
| 85 | + # unless it belongs to a nested ``{ ... }`` expression. |
| 86 | + 'interp': [ |
| 87 | + (r'\{', Punctuation, '#push'), |
| 88 | + (r'\}', String.Interpol, '#pop'), |
| 89 | + include('expr'), |
| 90 | + ], |
| 91 | + 'string': [ |
| 92 | + (r'\\u\([0-9A-Fa-f]{1,6}\)', String.Escape), |
| 93 | + (r'\\[$"nrt\\]', String.Escape), |
| 94 | + (r'\$\{', String.Interpol, 'interp'), |
| 95 | + (r'[^"$\\]+', String), |
| 96 | + (r'[$\\]', String), |
| 97 | + (r'"', String, '#pop'), |
| 98 | + ], |
| 99 | + # One line of a block string; ``\\`` was already consumed. The |
| 100 | + # next line, if it also starts with ``\\``, re-enters this state. |
| 101 | + 'blockstring': [ |
| 102 | + (r'\\u\([0-9A-Fa-f]{1,6}\)', String.Escape), |
| 103 | + (r'\\[$"nrt\\]', String.Escape), |
| 104 | + (r'\$\{', String.Interpol, 'interp'), |
| 105 | + (r'[^\n$"\\]+', String), |
| 106 | + (r'\n', String, '#pop'), |
| 107 | + (r'[$\\]', String), |
| 108 | + ], |
| 109 | + } |
0 commit comments