summaryrefslogtreecommitdiff
path: root/src/parser/Python3Lexer.g4
blob: 8d533013cfbb8860f0d091db78f8d337a1ec83f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 La grammatica di Python si trova a
 https://docs.python.org/3/reference/grammar.html

 Questa e` stata elaborata da  Bart Kiers, bart@big-o.nl
 e si trova a https://github.com/bkiers/python3-parser

 Semplificata ai fini del corso di CLP -- Marco Bertoni, Cosimo Laneve
*/

// $antlr-format alignTrailingComments true, columnLimit 150, maxEmptyLinesToKeep 1, reflowComments false, useTab false
// $antlr-format allowShortRulesOnASingleLine true, allowShortBlocksOnASingleLine true, minEmptyLines 0, alignSemicolons ownLine
// $antlr-format alignColons trailing, singleLineOverrulesHangingColon true, alignLexerCommands true, alignLabels true, alignTrailers true

lexer grammar Python3Lexer;

tokens {
    INDENT,
    DEDENT
}

options {
    superClass = Python3LexerBase;
}

STRING  : '\'' (STRING_ESCAPE_SEQ | ~[\\\r\n\f'])* '\''
        | '"' ( STRING_ESCAPE_SEQ | ~[\\\r\n\f"])* '"'
        ;

NUMBER : DECIMAL_INTEGER | FLOAT_NUMBER ;

AND        : 'and';
AS         : 'as';
DEF        : 'def';
ELIF       : 'elif';
ELSE       : 'else';
FALSE      : 'False';
FOR        : 'for';
FROM       : 'from';
IF         : 'if';
IMPORT     : 'import';
IN         : 'in';
IS         : 'is';
NONE       : 'None';
NOT        : 'not';
OR         : 'or';
RETURN     : 'return';
TRUE       : 'True';
UNDERSCORE : '_';
WHILE      : 'while';

NEWLINE: ({this.atStartOfInput()}? SPACES | ( '\r'? '\n' | '\r' | '\f') SPACES?) {this.onNewLine();};

NAME: ('_' | 'a'..'z' | 'A'..'Z') ('_' | 'a'..'z' | 'A'..'Z' | '0'..'9')* 
     ;

DECIMAL_INTEGER: NON_ZERO_DIGIT DIGIT* | '0'+;

FLOAT_NUMBER: POINT_FLOAT | EXPONENT_FLOAT;

DOT                : '.';
ELLIPSIS           : '...';
STAR               : '*';
OPEN_PAREN         : '(' {this.openBrace();};
CLOSE_PAREN        : ')' {this.closeBrace();};
COMMA              : ',';
COLON              : ':';
SEMI_COLON         : ';';
POWER              : '**';
ASSIGN             : '=';
OPEN_BRACK         : '[' {this.openBrace();};
CLOSE_BRACK        : ']' {this.closeBrace();};
OR_OP              : '|';
XOR                : '^';
AND_OP             : '&';
LEFT_SHIFT         : '<<';
RIGHT_SHIFT        : '>>';
ADD                : '+';
MINUS              : '-';
DIV                : '/';
MOD                : '%';
IDIV               : '//';
NOT_OP             : '~';
OPEN_BRACE         : '{' {this.openBrace();};
CLOSE_BRACE        : '}' {this.closeBrace();};
LESS_THAN          : '<';
GREATER_THAN       : '>';
EQUALS             : '==';
GT_EQ              : '>=';
LT_EQ              : '<=';
NOT_EQ_1           : '<>';
NOT_EQ_2           : '!=';
AT                 : '@';
ARROW              : '->';
ADD_ASSIGN         : '+=';
SUB_ASSIGN         : '-=';
MULT_ASSIGN        : '*=';
AT_ASSIGN          : '@=';
DIV_ASSIGN         : '/=';
MOD_ASSIGN         : '%=';
AND_ASSIGN         : '&=';
OR_ASSIGN          : '|=';
XOR_ASSIGN         : '^=';
LEFT_SHIFT_ASSIGN  : '<<=';
RIGHT_SHIFT_ASSIGN : '>>=';
POWER_ASSIGN       : '**=';
IDIV_ASSIGN        : '//=';

SKIP_: ( SPACES | COMMENT | LINE_JOINING) -> skip;

UNKNOWN_CHAR: .;

fragment STRING_ESCAPE_SEQ: '\\' . | '\\' NEWLINE;

fragment NON_ZERO_DIGIT: [1-9];

fragment DIGIT: [0-9];

fragment POINT_FLOAT: INT_PART? '.' DIGIT+ | INT_PART '.';

fragment EXPONENT_FLOAT: ( INT_PART | POINT_FLOAT) EXPONENT;

fragment INT_PART: DIGIT+;

fragment EXPONENT: [eE] [+-]? DIGIT+;

fragment SPACES: [ \t]+;

fragment COMMENT: '#' ~[\r\n\f]*;

fragment LINE_JOINING: '\\' SPACES? ( '\r'? '\n' | '\r' | '\f');