contrib: bring in the pep8 analysis framework
[nit.git] / contrib / pep8analysis / src / parser / pep8.sablecc
1 /*
2  * This file is part of the pep8analyser project.
3  *
4  * Copyright 2013 Alexis Laferriere <alexis.laf@xymus.net>
5  *
6  * Inspired from the Nit language grammar by:
7  * Copyright 2008-2009 Jean Privat <jean@pryen.org>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 /* This grammar defines the Pep/8 language. */
23 Grammar pep8;
24
25 /*****************************************************************************/
26 Lexer
27 /*****************************************************************************/
28
29 all = ' ' .. '~';
30 lowercase = 'a' .. 'z';
31 uppercase = 'A' .. 'Z';
32 digit = '0' .. '9';
33 letter = lowercase | uppercase | digit | '_';
34
35 tab = '\t';
36 cr = '\n';
37 lf = '\r';
38 any = all - (cr | lf);
39
40 str_char
41         = (any - '"') + '\\'
42         | '\\' any;
43
44 eol_helper = cr lf | cr | lf; // This takes care of different platforms;
45
46 hex_digit = '0' .. '9' | 'A' .. 'F' | 'a' .. 'f';
47
48 blank = (' ' | tab)+;
49
50 eol = eol_helper;
51
52 number = '-'? digit+;
53 hex = '0' ('x'|'X') hex_digit+;
54 float = digit* '.' digit+;
55 char = ('\'' ((any - '\'') - '\\') '\'')
56         | ('\'' '\\' any '\'')
57         | ('\'' '\\' 'x' hex_digit hex_digit '\'');
58 string = '"' str_char* '"';
59
60 colon = ':';
61 comma = ',';
62 comment = ';' any*;
63
64 tk_byte = '.' ('B'|'b') ('Y'|'y') ('T'|'t') ('E'|'e');
65 tk_word = '.' ('W'|'w') ('O'|'o') ('R'|'r') ('D'|'d');
66 tk_block = '.' ('B'|'b') ('L'|'l') ('O'|'o') ('C'|'c') ('K'|'k');
67 tk_ascii = '.' ('A'|'a') ('S'|'s') ('C'|'c') ('I'|'i') ('I'|'i');
68 tk_addrss = '.' ('A'|'a') ('D'|'d') ('D'|'d') ('R'|'r') ('S'|'s') ('S'|'s');
69 tk_equate = '.' ('E'|'e') ('Q'|'q') ('U'|'u') ('A'|'a') ('T'|'t') ('E'|'e');
70 tk_burn = '.' ('B'|'b') ('U'|'u') ('R'|'r') ('N'|'n');
71
72 end_block = '.' ('E'|'e') ('N'|'n') ('D'|'d') (any | eol_helper)*;
73
74 id = (lowercase|uppercase|'_') letter*;
75
76 /*****************************************************************************/
77 Parser
78 /*****************************************************************************/
79 Ignored blank;
80 listing = [lines]:line* label_decl? end_block;
81
82 line
83         = {empty} label_decl? comment? eol
84         | {instruction} label_decl? instruction comment? eol
85         | {directive} label_decl? directive comment? eol;
86
87 label_decl = id colon;
88
89 instruction
90         = {unary} id
91         | {binary} id operand;
92
93 /* operands
94  * We will manage which operands are possible with each stmt at a higher level.
95  * This will allow better error messages and a cleaner model. */
96 operand
97         = {immediate} value
98         | {any} value comma id;
99
100 value
101         = {label} id
102         | {char} char
103         | {string} string;
104         /*| {hex} hex;*/
105 directive
106         = {byte} tk_byte value
107         | {word} tk_word value
108         | {block} tk_block value
109         | {ascii} tk_ascii value
110         | {addrss} tk_addrss value
111         | {equate} tk_equate value
112         | {burn} tk_burn value;
113