| 1 |
//Copyright 2013 Vastech SA (PTY) LTD |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/*** Syntactic items ***/ |
| 16 |
|
| 17 |
<< import "github.com/goccmack/gocc/internal/ast" >> |
| 18 |
|
| 19 |
Grammar : LexicalPart SyntaxPart << ast.NewGrammar($0, $1) >> |
| 20 |
| LexicalPart << ast.NewGrammar($0, nil) >> |
| 21 |
| SyntaxPart << ast.NewGrammar(nil, $0) >> |
| 22 |
; |
| 23 |
|
| 24 |
LexicalPart : LexProductions << ast.NewLexPart(nil, nil, $0) >> |
| 25 |
; |
| 26 |
|
| 27 |
|
| 28 |
LexProductions : LexProduction << ast.NewLexProductions($0) >> |
| 29 |
| LexProductions LexProduction << ast.AppendLexProduction($0, $1) >> |
| 30 |
; |
| 31 |
|
| 32 |
LexProduction : tokId ":" LexPattern ";" << ast.NewLexTokDef($0, $2) >> |
| 33 |
| regDefId ":" LexPattern ";" << ast.NewLexRegDef($0, $2) >> |
| 34 |
| ignoredTokId ":" LexPattern ";" << ast.NewLexIgnoredTokDef($0, $2) >> |
| 35 |
; |
| 36 |
|
| 37 |
LexPattern : LexAlt << ast.NewLexPattern($0) >> |
| 38 |
| LexPattern "|" LexAlt << ast.AppendLexAlt($0, $2) >> |
| 39 |
; |
| 40 |
|
| 41 |
LexAlt : LexTerm << ast.NewLexAlt($0) >> |
| 42 |
| LexAlt LexTerm << ast.AppendLexTerm($0, $1) >> |
| 43 |
; |
| 44 |
|
| 45 |
LexTerm : "." << ast.LexDOT, nil >> |
| 46 |
| char_lit << ast.NewLexCharLit($0) >> |
| 47 |
| char_lit "-" char_lit << ast.NewLexCharRange($0, $2) >> |
| 48 |
| regDefId << ast.NewLexRegDefId($0) >> |
| 49 |
| "[" LexPattern "]" << ast.NewLexOptPattern($1) >> |
| 50 |
| "{" LexPattern "}" << ast.NewLexRepPattern($1) >> |
| 51 |
| "(" LexPattern ")" << ast.NewLexGroupPattern($1) >> |
| 52 |
; |
| 53 |
|
| 54 |
SyntaxPart : FileHeader SyntaxProdList << ast.NewSyntaxPart($0, $1) >> |
| 55 |
| SyntaxProdList << ast.NewSyntaxPart(nil, $0) >> |
| 56 |
; |
| 57 |
|
| 58 |
FileHeader : g_sdt_lit << ast.NewFileHeader($0) >> |
| 59 |
; |
| 60 |
|
| 61 |
SyntaxProdList : SyntaxProduction << ast.NewSyntaxProdList($0) >> |
| 62 |
| SyntaxProdList SyntaxProduction << ast.AddSyntaxProds($0, $1) >> |
| 63 |
; |
| 64 |
|
| 65 |
SyntaxProduction : prodId ":" Alternatives ";" << ast.NewSyntaxProd($0, $2) >> |
| 66 |
; |
| 67 |
|
| 68 |
Alternatives : SyntaxBody << ast.NewSyntaxAlts($0) >> |
| 69 |
| Alternatives "|" SyntaxBody << ast.AddSyntaxAlt($0, $2) >> |
| 70 |
; |
| 71 |
|
| 72 |
SyntaxBody : Symbols << ast.NewSyntaxBody($0, nil) >> |
| 73 |
| Symbols g_sdt_lit << ast.NewSyntaxBody($0, $1) >> |
| 74 |
| "error" << ast.NewErrorBody(nil, nil) >> |
| 75 |
| "error" Symbols << ast.NewErrorBody($1, nil) >> |
| 76 |
| "error" Symbols g_sdt_lit << ast.NewErrorBody($1, $2) >> |
| 77 |
| "empty" << ast.NewEmptyBody() >> |
| 78 |
; |
| 79 |
|
| 80 |
Symbols : Symbol << ast.NewSyntaxSymbols($0) >> |
| 81 |
| Symbols Symbol << ast.AddSyntaxSymbol($0, $1) >> |
| 82 |
; |
| 83 |
|
| 84 |
Symbol : prodId << ast.NewSyntaxProdId($0) >> |
| 85 |
| tokId << ast.NewTokId($0) >> |
| 86 |
| string_lit << ast.NewStringLit($0) >> |
| 87 |
; |
| 88 |
|
| 89 |
/******** |
| 90 |
Lexical items |
| 91 |
The basic unit of input to the lexical analyser is a UTF-8 encoded Unicode code point, defined as: |
| 92 |
|
| 93 |
_unicode_char : < any Unicode code point > . |
| 94 |
|
| 95 |
_letter : 'A' ... 'Z' | 'a' ... 'z' | '_' ; |
| 96 |
|
| 97 |
_digit : '0' ... '9' ; |
| 98 |
|
| 99 |
char : "'" ( _unicode_value | _byte_value ) "'" ; |
| 100 |
|
| 101 |
_unicode_value : _unicode_char | _little_u_value | _big_u_value | _escaped_char ; |
| 102 |
_byte_value : _octal_byte_value | _hex_byte_value ; |
| 103 |
_octal_byte_value : `\` _octal_digit _octal_digit _octal_digit ; |
| 104 |
_hex_byte_value : `\` "x" _hex_digit _hex_digit ; |
| 105 |
_little_u_value : `\` "u" _hex_digit _hex_digit _hex_digit _hex_digit ; |
| 106 |
_big_u_value : `\` "U" _hex_digit _hex_digit _hex_digit _hex_digit |
| 107 |
_hex_digit _hex_digit _hex_digit _hex_digit ; |
| 108 |
_escaped_char : `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | `'` | `"` ) ; |
| 109 |
|
| 110 |
id : _letter (_letter | _digit)* ; |
| 111 |
|
| 112 |
string : _raw_string | _interpreted_string ; |
| 113 |
_raw_string : "`" _unicode_char* "`" ; |
| 114 |
_interpreted_string : `"` ( _unicode_value | byte_value )* `"` ; |
| 115 |
|
| 116 |
g_sdt_lit : '<' '<' _unicode_char+ '>' '>' |
| 117 |
**********/ |
| 118 |
|
| 119 |
/*** TODO: *** |
| 120 |
1. Handle reserved words correctly so that user cannot write reserved words in his grammar. E.g.: string_lit, prodId, etc. |
| 121 |
***/ |