94b4b03b4dd566ba5040e315c159c0b2907016d7
[ibg.git] / tools / inform.py
1 """
2 Inform 6 syntax highlighting.
3
4 This is an ultra-minimal version compared to the standard Pygments Inform6
5 lexer, but is much more forgiving of syntax errors.  And it handles the
6 exclamation-inside-string case properly.
7 """
8
9 from pygments.lexer import RegexLexer
10 from pygments.token import (Text, Comment, Operator, Keyword, Name,
11                             String, Number, Punctuation, Token)
12
13 objects = ["Class", "Nearby", "Object"]
14
15 directives = ["Abbreviate", "Array", "Attribute", "Btrace", "Class",
16               "Constant", "Default", "Dictionary", "End", "Endif", "Etrace",
17               "Extend", "Fake_action", "Global", "Ifdef", "Iffalse",
18               "Ifndef", "Ifnot", "Iftrue", "Ifv3", "Ifv5", "Import",
19               "Include", "Link", "Listsymbols", "Listdict", "Listverbs",
20               "Lowstring", "Ltrace", "Message", "Nearby", "Nobtrace",
21               "Noetrace", "Noltrace", "Notrace", "Object", "Property",
22               "Release", "Replace", "Serial", "Statusline", "Stub",
23               "Switches", "System_file", "Trace", "Verb", "Version",
24               "Zcharacter"]
25
26 defining = ["[", "array", "attribute", "class", "constant", "fake_action",
27             "global", "lowstring", "nearby", "object", "property"]
28
29 attributes = ["absent", "animate", "clothing", "concealed", "container",
30               "door", "edible", "enterable", "female", "general", "light",
31               "lockable", "locked", "male", "moved", "neuter", "on", "open",
32               "openable", "pluralname", "proper", "scenery", "scored",
33               "static", "supporter", "switchable", "talkable", "transparent",
34               "visited", "workflag", "worn"]
35
36 properties = ["n_to", "s_to", "e_to", "w_to", "ne_to", "se_to", "nw_to",
37               "sw_to", "u_to", "d_to", "in_to", "out_to", "add_to_scope",
38               "after", "article", "articles", "before", "cant_go", "capacity",
39               "daemon", "describe", "description", "door_dir", "door_to",
40               "each_turn", "found_in", "grammar", "initial",
41               "inside_description", "invent", "life", "list_together",
42               "name", "number", "orders", "parse_name", "plural",
43               "react_after", "react_before", "short_name", "short_name_indef",
44               "time_left", "time_out", "when_closed", "when_open", "when_on",
45               "when_off", "with_key"]
46
47 keywords = ["box", "break", "continue", "do", "else", "font off", "font on",
48             "for", "give", "has", "hasnt", "if", "in", "inversion", "jump",
49             "move", "new_line", "notin", "objectloop", "ofclass", "or",
50             "print", "print_ret", "provides", "quit", "read", "remove",
51             "restore", "return", "rfalse", "rtrue", "save", "spaces",
52             "string", "style bold", "style fixed", "style reverse",
53             "style roman", "style underline", "switch", "to", "until",
54             "while", "with"]
55
56 constants = ["false", "true"]
57
58 def wordlist(list):
59     return "(" + "|".join(list) + r")\b"
60
61 class InformLexer(RegexLexer):
62     """
63     Inform code lexer.
64     """
65
66     name = 'Inform 6'
67     aliases = ['inform', 'inform6', 'i6']
68     filenames = ['*.inf']
69     mimetypes = ['text/x-inform', 'application/x-inform']
70
71     tokens = {
72         'root': [
73             (r'"', String.Double, 'stringdouble'),
74             (r"'", String.Single, 'stringsingle'),
75
76             (r'\n', Text),
77             (r'[^\S\n]+', Text),
78             (r'!.*$', Comment.Single),
79             (r'\\\n', Text),
80             (r'\\', Text),
81             (r'=', Operator),
82             (r"[A-Za-z_,]+:", Name.Label),
83             (r"<<\S+>>", Name.Label),
84
85             (wordlist(objects), Name.Class),
86             (wordlist(keywords), Token.Keyword.Reserved),
87             (wordlist(properties), Name.Builtin),
88             (wordlist(directives), Name.Entity),
89             (wordlist(attributes), Name.Attribute),
90             (wordlist(constants), Name.Constant),
91
92             (r'[a-zA-Z_][a-zA-Z0-9_.]*', Name),
93             (r'(\d+\.?\d*|\d*\.\d+)([eE][+-]?[0-9]+)?', Number.Float),
94             (r'\d+', Number.Integer),
95
96             (r'.', Punctuation),
97         ],
98
99         'stringdouble': [
100             (r'"', String.Double, '#pop'),
101             (r'.', String.Double),
102             (r'\n', String.Double),
103         ],
104
105         'stringsingle': [
106             (r"'", String.Single, '#pop'),
107             (r'.', String.Single),
108         ],
109     }