ThirdParty/Pygments/pygments/lexers/dsls.py

changeset 5713
6762afd9f963
parent 4697
c2e9bf425554
child 6651
e8f3b5568b21
equal deleted inserted replaced
5712:f0d08bdeacf4 5713:6762afd9f963
3 pygments.lexers.dsls 3 pygments.lexers.dsls
4 ~~~~~~~~~~~~~~~~~~~~ 4 ~~~~~~~~~~~~~~~~~~~~
5 5
6 Lexers for various domain-specific languages. 6 Lexers for various domain-specific languages.
7 7
8 :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS. 8 :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details. 9 :license: BSD, see LICENSE for details.
10 """ 10 """
11 11
12 import re 12 import re
13 13
14 from pygments.lexer import RegexLexer, bygroups, words, include, default, \ 14 from pygments.lexer import ExtendedRegexLexer, RegexLexer, bygroups, words, \
15 this, using, combined 15 include, default, this, using, combined
16 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ 16 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
17 Number, Punctuation, Literal, Whitespace 17 Number, Punctuation, Literal, Whitespace
18 18
19 __all__ = ['ProtoBufLexer', 'BroLexer', 'PuppetLexer', 'RslLexer', 19 __all__ = ['ProtoBufLexer', 'BroLexer', 'PuppetLexer', 'RslLexer',
20 'MscgenLexer', 'VGLLexer', 'AlloyLexer', 'PanLexer', 20 'MscgenLexer', 'VGLLexer', 'AlloyLexer', 'PanLexer',
21 'CrmshLexer', 'ThriftLexer'] 21 'CrmshLexer', 'ThriftLexer', 'FlatlineLexer', 'SnowballLexer']
22 22
23 23
24 class ProtoBufLexer(RegexLexer): 24 class ProtoBufLexer(RegexLexer):
25 """ 25 """
26 Lexer for `Protocol Buffer <http://code.google.com/p/protobuf/>`_ 26 Lexer for `Protocol Buffer <http://code.google.com/p/protobuf/>`_
34 filenames = ['*.proto'] 34 filenames = ['*.proto']
35 35
36 tokens = { 36 tokens = {
37 'root': [ 37 'root': [
38 (r'[ \t]+', Text), 38 (r'[ \t]+', Text),
39 (r'[,;{}\[\]()]', Punctuation), 39 (r'[,;{}\[\]()<>]', Punctuation),
40 (r'/(\\\n)?/(\n|(.|\n)*?[^\\]\n)', Comment.Single), 40 (r'/(\\\n)?/(\n|(.|\n)*?[^\\]\n)', Comment.Single),
41 (r'/(\\\n)?\*(.|\n)*?\*(\\\n)?/', Comment.Multiline), 41 (r'/(\\\n)?\*(.|\n)*?\*(\\\n)?/', Comment.Multiline),
42 (words(( 42 (words((
43 'import', 'option', 'optional', 'required', 'repeated', 'default', 43 'import', 'option', 'optional', 'required', 'repeated', 'default',
44 'packed', 'ctype', 'extensions', 'to', 'max', 'rpc', 'returns', 44 'packed', 'ctype', 'extensions', 'to', 'max', 'rpc', 'returns',
109 r'(\s*)(\()', # signature start 109 r'(\s*)(\()', # signature start
110 bygroups(using(this), Name.Function, Text, Operator)), 110 bygroups(using(this), Name.Function, Text, Operator)),
111 include('keywords'), 111 include('keywords'),
112 include('numbers'), 112 include('numbers'),
113 (r'[&=]', Operator), 113 (r'[&=]', Operator),
114 (r'[:;\,\{\}\(\)\<>\[\]]', Punctuation), 114 (r'[:;,{}()<>\[\]]', Punctuation),
115 (r'[a-zA-Z_](\.[a-zA-Z_0-9]|[a-zA-Z_0-9])*', Name), 115 (r'[a-zA-Z_](\.\w|\w)*', Name),
116 ], 116 ],
117 'whitespace': [ 117 'whitespace': [
118 (r'\n', Text.Whitespace), 118 (r'\n', Text.Whitespace),
119 (r'\s+', Text.Whitespace), 119 (r'\s+', Text.Whitespace),
120 ], 120 ],
133 'sqs': [ 133 'sqs': [
134 (r"'", String.Single, '#pop'), 134 (r"'", String.Single, '#pop'),
135 (r'[^\\\'\n]+', String.Single), 135 (r'[^\\\'\n]+', String.Single),
136 ], 136 ],
137 'namespace': [ 137 'namespace': [
138 (r'[a-z\*](\.[a-zA-Z_0-9]|[a-zA-Z_0-9])*', Name.Namespace, '#pop'), 138 (r'[a-z*](\.\w|\w)*', Name.Namespace, '#pop'),
139 default('#pop'), 139 default('#pop'),
140 ], 140 ],
141 'class': [ 141 'class': [
142 (r'[a-zA-Z_]\w*', Name.Class, '#pop'), 142 (r'[a-zA-Z_]\w*', Name.Class, '#pop'),
143 default('#pop'), 143 default('#pop'),
154 'xsd_all', 'xsd_optional', 'xsd_nillable', 'xsd_namespace', 154 'xsd_all', 'xsd_optional', 'xsd_nillable', 'xsd_namespace',
155 'xsd_attrs', 'include'), suffix=r'\b'), 155 'xsd_attrs', 'include'), suffix=r'\b'),
156 Keyword.Namespace), 156 Keyword.Namespace),
157 (words(( 157 (words((
158 'void', 'bool', 'byte', 'i16', 'i32', 'i64', 'double', 158 'void', 'bool', 'byte', 'i16', 'i32', 'i64', 'double',
159 'string', 'binary', 'void', 'map', 'list', 'set', 'slist', 159 'string', 'binary', 'map', 'list', 'set', 'slist',
160 'senum'), suffix=r'\b'), 160 'senum'), suffix=r'\b'),
161 Keyword.Type), 161 Keyword.Type),
162 (words(( 162 (words((
163 'BEGIN', 'END', '__CLASS__', '__DIR__', '__FILE__', 163 'BEGIN', 'END', '__CLASS__', '__DIR__', '__FILE__',
164 '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__', 164 '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__',
579 'basic': [ 579 'basic': [
580 (words(( 580 (words((
581 'if', 'for', 'with', 'else', 'type', 'bind', 'while', 'valid', 'final', 581 'if', 'for', 'with', 'else', 'type', 'bind', 'while', 'valid', 'final',
582 'prefix', 'unique', 'object', 'foreach', 'include', 'template', 582 'prefix', 'unique', 'object', 'foreach', 'include', 'template',
583 'function', 'variable', 'structure', 'extensible', 'declaration'), 583 'function', 'variable', 'structure', 'extensible', 'declaration'),
584 prefix=r'\b', suffix=r'\s*\b'), 584 prefix=r'\b', suffix=r'\s*\b'),
585 Keyword), 585 Keyword),
586 (words(( 586 (words((
587 'file_contents', 'format', 'index', 'length', 'match', 'matches', 587 'file_contents', 'format', 'index', 'length', 'match', 'matches',
588 'replace', 'splice', 'split', 'substr', 'to_lowercase', 'to_uppercase', 588 'replace', 'splice', 'split', 'substr', 'to_lowercase', 'to_uppercase',
589 'debug', 'error', 'traceback', 'deprecated', 'base64_decode', 589 'debug', 'error', 'traceback', 'deprecated', 'base64_decode',
591 'first', 'nlist', 'key', 'list', 'merge', 'next', 'prepend', 'is_boolean', 591 'first', 'nlist', 'key', 'list', 'merge', 'next', 'prepend', 'is_boolean',
592 'is_defined', 'is_double', 'is_list', 'is_long', 'is_nlist', 'is_null', 592 'is_defined', 'is_double', 'is_list', 'is_long', 'is_nlist', 'is_null',
593 'is_number', 'is_property', 'is_resource', 'is_string', 'to_boolean', 593 'is_number', 'is_property', 'is_resource', 'is_string', 'to_boolean',
594 'to_double', 'to_long', 'to_string', 'clone', 'delete', 'exists', 594 'to_double', 'to_long', 'to_string', 'clone', 'delete', 'exists',
595 'path_exists', 'if_exists', 'return', 'value'), 595 'path_exists', 'if_exists', 'return', 'value'),
596 prefix=r'\b', suffix=r'\s*\b'), 596 prefix=r'\b', suffix=r'\s*\b'),
597 Name.Builtin), 597 Name.Builtin),
598 (r'#.*', Comment), 598 (r'#.*', Comment),
599 (r'\\[\w\W]', String.Escape), 599 (r'\\[\w\W]', String.Escape),
600 (r'(\b\w+)(\s*)(=)', bygroups(Name.Variable, Text, Operator)), 600 (r'(\b\w+)(\s*)(=)', bygroups(Name.Variable, Text, Operator)),
601 (r'[\[\]{}()=]+', Operator), 601 (r'[\[\]{}()=]+', Operator),
690 # punctuation 690 # punctuation
691 (r'(\\(?=\n)|[[\](){}/:@])', Punctuation), 691 (r'(\\(?=\n)|[[\](){}/:@])', Punctuation),
692 (r'\s+|\n', Whitespace), 692 (r'\s+|\n', Whitespace),
693 ], 693 ],
694 } 694 }
695
696
697 class FlatlineLexer(RegexLexer):
698 """
699 Lexer for `Flatline <https://github.com/bigmlcom/flatline>`_ expressions.
700
701 .. versionadded:: 2.2
702 """
703 name = 'Flatline'
704 aliases = ['flatline']
705 filenames = []
706 mimetypes = ['text/x-flatline']
707
708 special_forms = ('let',)
709
710 builtins = (
711 "!=", "*", "+", "-", "<", "<=", "=", ">", ">=", "abs", "acos", "all",
712 "all-but", "all-with-defaults", "all-with-numeric-default", "and",
713 "asin", "atan", "avg", "avg-window", "bin-center", "bin-count", "call",
714 "category-count", "ceil", "cond", "cond-window", "cons", "cos", "cosh",
715 "count", "diff-window", "div", "ensure-value", "ensure-weighted-value",
716 "epoch", "epoch-day", "epoch-fields", "epoch-hour", "epoch-millisecond",
717 "epoch-minute", "epoch-month", "epoch-second", "epoch-weekday",
718 "epoch-year", "exp", "f", "field", "field-prop", "fields", "filter",
719 "first", "floor", "head", "if", "in", "integer", "language", "length",
720 "levenshtein", "linear-regression", "list", "ln", "log", "log10", "map",
721 "matches", "matches?", "max", "maximum", "md5", "mean", "median", "min",
722 "minimum", "missing", "missing-count", "missing?", "missing_count",
723 "mod", "mode", "normalize", "not", "nth", "occurrences", "or",
724 "percentile", "percentile-label", "population", "population-fraction",
725 "pow", "preferred", "preferred?", "quantile-label", "rand", "rand-int",
726 "random-value", "re-quote", "real", "replace", "replace-first", "rest",
727 "round", "row-number", "segment-label", "sha1", "sha256", "sin", "sinh",
728 "sqrt", "square", "standard-deviation", "standard_deviation", "str",
729 "subs", "sum", "sum-squares", "sum-window", "sum_squares", "summary",
730 "summary-no", "summary-str", "tail", "tan", "tanh", "to-degrees",
731 "to-radians", "variance", "vectorize", "weighted-random-value", "window",
732 "winnow", "within-percentiles?", "z-score",
733 )
734
735 valid_name = r'(?!#)[\w!$%*+<=>?/.#-]+'
736
737 tokens = {
738 'root': [
739 # whitespaces - usually not relevant
740 (r'[,\s]+', Text),
741
742 # numbers
743 (r'-?\d+\.\d+', Number.Float),
744 (r'-?\d+', Number.Integer),
745 (r'0x-?[a-f\d]+', Number.Hex),
746
747 # strings, symbols and characters
748 (r'"(\\\\|\\"|[^"])*"', String),
749 (r"\\(.|[a-z]+)", String.Char),
750
751 # expression template placeholder
752 (r'_', String.Symbol),
753
754 # highlight the special forms
755 (words(special_forms, suffix=' '), Keyword),
756
757 # highlight the builtins
758 (words(builtins, suffix=' '), Name.Builtin),
759
760 # the remaining functions
761 (r'(?<=\()' + valid_name, Name.Function),
762
763 # find the remaining variables
764 (valid_name, Name.Variable),
765
766 # parentheses
767 (r'(\(|\))', Punctuation),
768 ],
769 }
770
771
772 class SnowballLexer(ExtendedRegexLexer):
773 """
774 Lexer for `Snowball <http://snowballstem.org/>`_ source code.
775
776 .. versionadded:: 2.2
777 """
778
779 name = 'Snowball'
780 aliases = ['snowball']
781 filenames = ['*.sbl']
782
783 _ws = r'\n\r\t '
784
785 def __init__(self, **options):
786 self._reset_stringescapes()
787 ExtendedRegexLexer.__init__(self, **options)
788
789 def _reset_stringescapes(self):
790 self._start = "'"
791 self._end = "'"
792
793 def _string(do_string_first):
794 def callback(lexer, match, ctx):
795 s = match.start()
796 text = match.group()
797 string = re.compile(r'([^%s]*)(.)' % re.escape(lexer._start)).match
798 escape = re.compile(r'([^%s]*)(.)' % re.escape(lexer._end)).match
799 pos = 0
800 do_string = do_string_first
801 while pos < len(text):
802 if do_string:
803 match = string(text, pos)
804 yield s + match.start(1), String.Single, match.group(1)
805 if match.group(2) == "'":
806 yield s + match.start(2), String.Single, match.group(2)
807 ctx.stack.pop()
808 break
809 yield s + match.start(2), String.Escape, match.group(2)
810 pos = match.end()
811 match = escape(text, pos)
812 yield s + match.start(), String.Escape, match.group()
813 if match.group(2) != lexer._end:
814 ctx.stack[-1] = 'escape'
815 break
816 pos = match.end()
817 do_string = True
818 ctx.pos = s + match.end()
819 return callback
820
821 def _stringescapes(lexer, match, ctx):
822 lexer._start = match.group(3)
823 lexer._end = match.group(5)
824 return bygroups(Keyword.Reserved, Text, String.Escape, Text,
825 String.Escape)(lexer, match, ctx)
826
827 tokens = {
828 'root': [
829 (words(('len', 'lenof'), suffix=r'\b'), Operator.Word),
830 include('root1'),
831 ],
832 'root1': [
833 (r'[%s]+' % _ws, Text),
834 (r'\d+', Number.Integer),
835 (r"'", String.Single, 'string'),
836 (r'[()]', Punctuation),
837 (r'/\*[\w\W]*?\*/', Comment.Multiline),
838 (r'//.*', Comment.Single),
839 (r'[!*+\-/<=>]=|[-=]>|<[+-]|[$*+\-/<=>?\[\]]', Operator),
840 (words(('as', 'get', 'hex', 'among', 'define', 'decimal',
841 'backwardmode'), suffix=r'\b'),
842 Keyword.Reserved),
843 (words(('strings', 'booleans', 'integers', 'routines', 'externals',
844 'groupings'), suffix=r'\b'),
845 Keyword.Reserved, 'declaration'),
846 (words(('do', 'or', 'and', 'for', 'hop', 'non', 'not', 'set', 'try',
847 'fail', 'goto', 'loop', 'next', 'test', 'true',
848 'false', 'unset', 'atmark', 'attach', 'delete', 'gopast',
849 'insert', 'repeat', 'sizeof', 'tomark', 'atleast',
850 'atlimit', 'reverse', 'setmark', 'tolimit', 'setlimit',
851 'backwards', 'substring'), suffix=r'\b'),
852 Operator.Word),
853 (words(('size', 'limit', 'cursor', 'maxint', 'minint'),
854 suffix=r'\b'),
855 Name.Builtin),
856 (r'(stringdef\b)([%s]*)([^%s]+)' % (_ws, _ws),
857 bygroups(Keyword.Reserved, Text, String.Escape)),
858 (r'(stringescapes\b)([%s]*)(.)([%s]*)(.)' % (_ws, _ws),
859 _stringescapes),
860 (r'[A-Za-z]\w*', Name),
861 ],
862 'declaration': [
863 (r'\)', Punctuation, '#pop'),
864 (words(('len', 'lenof'), suffix=r'\b'), Name,
865 ('root1', 'declaration')),
866 include('root1'),
867 ],
868 'string': [
869 (r"[^']*'", _string(True)),
870 ],
871 'escape': [
872 (r"[^']*'", _string(False)),
873 ],
874 }
875
876 def get_tokens_unprocessed(self, text=None, context=None):
877 self._reset_stringescapes()
878 return ExtendedRegexLexer.get_tokens_unprocessed(self, text, context)

eric ide

mercurial