merge
[cython.git] / Cython / Compiler / Lexicon.py
1 # cython: language_level=3, py2_import=True
2 #
3 #   Cython Scanner - Lexical Definitions
4 #
5
6 raw_prefixes = "rR"
7 string_prefixes = "cCuUbB"
8 IDENT = 'IDENT'
9
10 def make_lexicon():
11     from Cython.Plex import \
12         Str, Any, AnyBut, AnyChar, Rep, Rep1, Opt, Bol, Eol, Eof, \
13         TEXT, IGNORE, State, Lexicon
14     from Scanning import Method
15
16     letter = Any("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_")
17     digit = Any("0123456789")
18     bindigit = Any("01")
19     octdigit = Any("01234567")
20     hexdigit = Any("0123456789ABCDEFabcdef")
21     indentation = Bol + Rep(Any(" \t"))
22
23     decimal = Rep1(digit)
24     dot = Str(".")
25     exponent = Any("Ee") + Opt(Any("+-")) + decimal
26     decimal_fract = (decimal + dot + Opt(decimal)) | (dot + decimal)
27
28     name = letter + Rep(letter | digit)
29     intconst = decimal | (Str("0") + ((Any("Xx") + Rep1(hexdigit)) |
30                                       (Any("Oo") + Rep1(octdigit)) |
31                                       (Any("Bb") + Rep1(bindigit)) ))
32     intsuffix = (Opt(Any("Uu")) + Opt(Any("Ll")) + Opt(Any("Ll"))) | (Opt(Any("Ll")) + Opt(Any("Ll")) + Opt(Any("Uu")))
33     intliteral = intconst + intsuffix
34     fltconst = (decimal_fract + Opt(exponent)) | (decimal + exponent)
35     imagconst = (intconst | fltconst) + Any("jJ")
36
37     sq_string = (
38         Str("'") +
39         Rep(AnyBut("\\\n'") | (Str("\\") + AnyChar)) +
40         Str("'")
41     )
42
43     dq_string = (
44         Str('"') +
45         Rep(AnyBut('\\\n"') | (Str("\\") + AnyChar)) +
46         Str('"')
47     )
48
49     non_sq = AnyBut("'") | (Str('\\') + AnyChar)
50     tsq_string = (
51         Str("'''")
52         + Rep(non_sq | (Str("'") + non_sq) | (Str("''") + non_sq))
53         + Str("'''")
54     )
55
56     non_dq = AnyBut('"') | (Str('\\') + AnyChar)
57     tdq_string = (
58         Str('"""')
59         + Rep(non_dq | (Str('"') + non_dq) | (Str('""') + non_dq))
60         + Str('"""')
61     )
62
63     beginstring = Opt(Any(string_prefixes)) + Opt(Any(raw_prefixes)) + (Str("'") | Str('"') | Str("'''") | Str('"""'))
64     two_oct = octdigit + octdigit
65     three_oct = octdigit + octdigit + octdigit
66     two_hex = hexdigit + hexdigit
67     four_hex = two_hex + two_hex
68     escapeseq = Str("\\") + (two_oct | three_oct |
69                              Str('u') + four_hex | Str('x') + two_hex |
70                              Str('U') + four_hex + four_hex | AnyChar)
71
72
73     deco = Str("@")
74     bra = Any("([{")
75     ket = Any(")]}")
76     punct = Any(":,;+-*/|&<>=.%`~^?")
77     diphthong = Str("==", "<>", "!=", "<=", ">=", "<<", ">>", "**", "//",
78                     "+=", "-=", "*=", "/=", "%=", "|=", "^=", "&=",
79                     "<<=", ">>=", "**=", "//=", "->")
80     spaces = Rep1(Any(" \t\f"))
81     escaped_newline = Str("\\\n")
82     lineterm = Eol + Opt(Str("\n"))
83
84     comment = Str("#") + Rep(AnyBut("\n"))
85
86     return Lexicon([
87         (name, IDENT),
88         (intliteral, 'INT'),
89         (fltconst, 'FLOAT'),
90         (imagconst, 'IMAG'),
91         (deco, 'DECORATOR'),
92         (punct | diphthong, TEXT),
93
94         (bra, Method('open_bracket_action')),
95         (ket, Method('close_bracket_action')),
96         (lineterm, Method('newline_action')),
97
98         #(stringlit, 'STRING'),
99         (beginstring, Method('begin_string_action')),
100
101         (comment, IGNORE),
102         (spaces, IGNORE),
103         (escaped_newline, IGNORE),
104
105         State('INDENT', [
106             (comment + lineterm, Method('commentline')),
107             (Opt(spaces) + Opt(comment) + lineterm, IGNORE),
108             (indentation, Method('indentation_action')),
109             (Eof, Method('eof_action'))
110         ]),
111
112         State('SQ_STRING', [
113             (escapeseq, 'ESCAPE'),
114             (Rep1(AnyBut("'\"\n\\")), 'CHARS'),
115             (Str('"'), 'CHARS'),
116             (Str("\n"), Method('unclosed_string_action')),
117             (Str("'"), Method('end_string_action')),
118             (Eof, 'EOF')
119         ]),
120
121         State('DQ_STRING', [
122             (escapeseq, 'ESCAPE'),
123             (Rep1(AnyBut('"\n\\')), 'CHARS'),
124             (Str("'"), 'CHARS'),
125             (Str("\n"), Method('unclosed_string_action')),
126             (Str('"'), Method('end_string_action')),
127             (Eof, 'EOF')
128         ]),
129
130         State('TSQ_STRING', [
131             (escapeseq, 'ESCAPE'),
132             (Rep1(AnyBut("'\"\n\\")), 'CHARS'),
133             (Any("'\""), 'CHARS'),
134             (Str("\n"), 'NEWLINE'),
135             (Str("'''"), Method('end_string_action')),
136             (Eof, 'EOF')
137         ]),
138
139         State('TDQ_STRING', [
140             (escapeseq, 'ESCAPE'),
141             (Rep1(AnyBut('"\'\n\\')), 'CHARS'),
142             (Any("'\""), 'CHARS'),
143             (Str("\n"), 'NEWLINE'),
144             (Str('"""'), Method('end_string_action')),
145             (Eof, 'EOF')
146         ]),
147
148         (Eof, Method('eof_action'))
149         ],
150
151         # FIXME: Plex 1.9 needs different args here from Plex 1.1.4
152         #debug_flags = scanner_debug_flags,
153         #debug_file = scanner_dump_file
154         )
155