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