feat/schema: add parser

This commit is contained in:
mae
2026-01-26 20:52:51 -06:00
parent 2526d34f21
commit 5f9467e851
13 changed files with 2860 additions and 913 deletions

View File

@@ -4,11 +4,10 @@ _bin_digit: '0' - '1';
_oct_digit: _bin_digit | '2' - '7'; _oct_digit: _bin_digit | '2' - '7';
_dec_digit: _oct_digit | '8' - '9'; _dec_digit: _oct_digit | '8' - '9';
_hex_digit: _dec_digit | 'A' - 'F' | 'a' - 'f'; _hex_digit: _dec_digit | 'A' - 'F' | 'a' - 'f';
_negative: '-'; number: ['-' | '+'] '0' 'b' _bin_digit {_bin_digit | '_'}
number: [_negative] '0' 'b' _bin_digit {_bin_digit} | ['-' | '+'] '0' 'o' _oct_digit {_oct_digit | '_'}
| [_negative] '0' 'o' _oct_digit {_oct_digit} | ['-' | '+'] _dec_digit {_dec_digit | '_'}
| [_negative] _dec_digit {_dec_digit} | ['-' | '+'] '0' 'x' _hex_digit {_hex_digit | '_'};
| [_negative] '0' 'x' _hex_digit {_hex_digit};
_name_initial: 'A' - 'Z' | 'a' - 'z' | '_' | '~' | '!' | '@' | '#' | '$' | '%' | '^' | '&' | '*' | '-' | '_' | '+' | '=' | '?' | '/' | '.' | '\''; _name_initial: 'A' - 'Z' | 'a' - 'z' | '_' | '~' | '!' | '@' | '#' | '$' | '%' | '^' | '&' | '*' | '-' | '_' | '+' | '=' | '?' | '/' | '.' | '\'';
_name_char: _name_initial | _dec_digit; _name_char: _name_initial | _dec_digit;
@@ -16,3 +15,26 @@ name: _name_initial {_name_char};
!whitespace: ' ' | '\t' | '\n' | '\r'; !whitespace: ' ' | '\t' | '\n' | '\r';
!comment: ';' {.} '\n'; !comment: ';' {.} '\n';
<<>>
Schema: ExprList;
ExprList
: Expr
| ExprList Expr
;
ValList
: Val
| ValList Val
;
Val
: string
| number
| name
| Expr
;
Expr
: "(" name Val Val ")"
| "(" name Val ")"
| "(" name ")"
| "(" "." ValList ")"
;

108
schema/errors/errors.go Normal file
View File

@@ -0,0 +1,108 @@
// Code generated by gocc; DO NOT EDIT.
package errors
import (
"fmt"
"strconv"
"strings"
"unicode"
"azalea/schema/token"
)
type ErrorSymbol interface {
}
type Error struct {
Err error
ErrorToken *token.Token
ErrorSymbols []ErrorSymbol
ExpectedTokens []string
StackTop int
}
func (e *Error) String() string {
w := new(strings.Builder)
if e.Err != nil {
fmt.Fprintln(w, "Error ", e.Err)
} else {
fmt.Fprintln(w, "Error")
}
fmt.Fprintf(w, "Token: type=%d, lit=%s\n", e.ErrorToken.Type, e.ErrorToken.Lit)
fmt.Fprintf(w, "Pos: offset=%d, line=%d, column=%d\n", e.ErrorToken.Pos.Offset, e.ErrorToken.Pos.Line, e.ErrorToken.Pos.Column)
fmt.Fprint(w, "Expected one of: ")
for _, sym := range e.ExpectedTokens {
fmt.Fprint(w, string(sym), " ")
}
fmt.Fprintln(w, "ErrorSymbol:")
for _, sym := range e.ErrorSymbols {
fmt.Fprintf(w, "%v\n", sym)
}
return w.String()
}
func DescribeExpected(tokens []string) string {
switch len(tokens) {
case 0:
return "unexpected additional tokens"
case 1:
return "expected " + tokens[0]
case 2:
return "expected either " + tokens[0] + " or " + tokens[1]
case 3:
// Oxford-comma rules require more than 3 items in a list for the
// comma to appear before the 'or'
return fmt.Sprintf("expected one of %s, %s or %s", tokens[0], tokens[1], tokens[2])
default:
// Oxford-comma separated alternatives list.
tokens = append(tokens[:len(tokens)-1], "or "+tokens[len(tokens)-1])
return "expected one of " + strings.Join(tokens, ", ")
}
}
func DescribeToken(tok *token.Token) string {
switch tok.Type {
case token.INVALID:
return fmt.Sprintf("unknown/invalid token %q", tok.Lit)
case token.EOF:
return "end-of-file"
default:
return fmt.Sprintf("%q", tok.Lit)
}
}
func (e *Error) Error() string {
// identify the line and column of the error in 'gnu' style so it can be understood
// by editors and IDEs; user will need to prefix it with a filename.
text := fmt.Sprintf("%d:%d: error: ", e.ErrorToken.Pos.Line, e.ErrorToken.Pos.Column)
// See if the error token can provide us with the filename.
switch src := e.ErrorToken.Pos.Context.(type) {
case token.Sourcer:
text = src.Source() + ":" + text
}
if e.Err != nil {
// Custom error specified, e.g. by << nil, errors.New("missing newline") >>
text += e.Err.Error()
} else {
tokens := make([]string, len(e.ExpectedTokens))
for idx, token := range e.ExpectedTokens {
if !unicode.IsLetter(rune(token[0])) {
token = strconv.Quote(token)
}
tokens[idx] = token
}
text += DescribeExpected(tokens)
actual := DescribeToken(e.ErrorToken)
text += fmt.Sprintf("; got: %s", actual)
}
return text
}

View File

@@ -29,7 +29,7 @@ var ActTab = ActionTable{
Ignore: "!whitespace", Ignore: "!whitespace",
}, },
ActionRow{ // S2 ActionRow{ // S2
Accept: 2, Accept: 4,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S3 ActionRow{ // S3
@@ -37,19 +37,19 @@ var ActTab = ActionTable{
Ignore: "", Ignore: "",
}, },
ActionRow{ // S4 ActionRow{ // S4
Accept: 2, Accept: 5,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S5 ActionRow{ // S5
Accept: 3, Accept: 6,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S6 ActionRow{ // S6
Accept: 3, Accept: 4,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S7 ActionRow{ // S7
Accept: 3, Accept: 7,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S8 ActionRow{ // S8
@@ -57,27 +57,27 @@ var ActTab = ActionTable{
Ignore: "", Ignore: "",
}, },
ActionRow{ // S9 ActionRow{ // S9
Accept: 0, Accept: 3,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S10 ActionRow{ // S10
Accept: 0, Accept: 3,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S11 ActionRow{ // S11
Accept: 2, Accept: 3,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S12 ActionRow{ // S12
Accept: 2, Accept: 0,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S13 ActionRow{ // S13
Accept: 2, Accept: 0,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S14 ActionRow{ // S14
Accept: 2, Accept: 4,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S15 ActionRow{ // S15
@@ -85,15 +85,15 @@ var ActTab = ActionTable{
Ignore: "", Ignore: "",
}, },
ActionRow{ // S16 ActionRow{ // S16
Accept: 3, Accept: 4,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S17 ActionRow{ // S17
Accept: 3, Accept: 4,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S18 ActionRow{ // S18
Accept: 3, Accept: 2,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S19 ActionRow{ // S19
@@ -101,51 +101,51 @@ var ActTab = ActionTable{
Ignore: "", Ignore: "",
}, },
ActionRow{ // S20 ActionRow{ // S20
Accept: 0, Accept: 3,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S21 ActionRow{ // S21
Accept: 0, Accept: 3,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S22 ActionRow{ // S22
Accept: 0, Accept: 3,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S23 ActionRow{ // S23
Accept: -1, Accept: 3,
Ignore: "!comment", Ignore: "",
}, },
ActionRow{ // S24 ActionRow{ // S24
Accept: 2, Accept: 0,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S25 ActionRow{ // S25
Accept: 2, Accept: 0,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S26 ActionRow{ // S26
Accept: 2, Accept: 0,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S27 ActionRow{ // S27
Accept: 3, Accept: -1,
Ignore: "", Ignore: "!comment",
}, },
ActionRow{ // S28 ActionRow{ // S28
Accept: 3, Accept: 3,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S29 ActionRow{ // S29
Accept: 3, Accept: 4,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S30 ActionRow{ // S30
Accept: 3, Accept: 4,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S31 ActionRow{ // S31
Accept: 3, Accept: 4,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S32 ActionRow{ // S32
@@ -184,4 +184,48 @@ var ActTab = ActionTable{
Accept: 3, Accept: 3,
Ignore: "", Ignore: "",
}, },
ActionRow{ // S41
Accept: 3,
Ignore: "",
},
ActionRow{ // S42
Accept: 3,
Ignore: "",
},
ActionRow{ // S43
Accept: 3,
Ignore: "",
},
ActionRow{ // S44
Accept: 3,
Ignore: "",
},
ActionRow{ // S45
Accept: 3,
Ignore: "",
},
ActionRow{ // S46
Accept: 3,
Ignore: "",
},
ActionRow{ // S47
Accept: 3,
Ignore: "",
},
ActionRow{ // S48
Accept: 3,
Ignore: "",
},
ActionRow{ // S49
Accept: 3,
Ignore: "",
},
ActionRow{ // S50
Accept: 3,
Ignore: "",
},
ActionRow{ // S51
Accept: 3,
Ignore: "",
},
} }

View File

@@ -11,8 +11,8 @@ import (
const ( const (
NoState = -1 NoState = -1
NumStates = 41 NumStates = 52
NumSymbols = 43 NumSymbols = 57
) )
type Lexer struct { type Lexer struct {
@@ -133,43 +133,57 @@ Lexer symbols:
1: '`' 1: '`'
2: '"' 2: '"'
3: '"' 3: '"'
4: '0' 4: '-'
5: 'b' 5: '+'
6: '0' 6: '0'
7: 'o' 7: 'b'
8: '0' 8: '_'
9: 'x' 9: '-'
10: '-' 10: '+'
11: '_' 11: '0'
12: '~' 12: 'o'
13: '!' 13: '_'
14: '@' 14: '-'
15: '#' 15: '+'
16: '$' 16: '_'
17: '%' 17: '-'
18: '^' 18: '+'
19: '&' 19: '0'
20: '*' 20: 'x'
21: '-' 21: '_'
22: '_' 22: '('
23: '+' 23: ')'
24: '=' 24: '.'
25: '?' 25: '_'
26: '/' 26: '~'
27: '.' 27: '!'
28: ''' 28: '@'
29: ' ' 29: '#'
30: '\t' 30: '$'
31: '\n' 31: '%'
32: '\r' 32: '^'
33: ';' 33: '&'
34: '\n' 34: '*'
35: '0'-'1' 35: '-'
36: '2'-'7' 36: '_'
37: '8'-'9' 37: '+'
38: 'A'-'F' 38: '='
39: 'a'-'f' 39: '?'
40: 'A'-'Z' 40: '/'
41: 'a'-'z' 41: '.'
42: . 42: '''
43: ' '
44: '\t'
45: '\n'
46: '\r'
47: ';'
48: '\n'
49: '0'-'1'
50: '2'-'7'
51: '8'-'9'
52: 'A'-'F'
53: 'a'-'f'
54: 'A'-'Z'
55: 'a'-'z'
56: .
*/ */

File diff suppressed because it is too large Load Diff

View File

@@ -1,2 +1,2 @@
//go:generate gocc azschema.bnf //go:generate gocc -a azschema.bnf
package schema package schema

51
schema/parser/action.go Normal file
View File

@@ -0,0 +1,51 @@
// Code generated by gocc; DO NOT EDIT.
package parser
import (
"fmt"
)
type action interface {
act()
String() string
}
type (
accept bool
shift int // value is next state index
reduce int // value is production index
)
func (this accept) act() {}
func (this shift) act() {}
func (this reduce) act() {}
func (this accept) Equal(that action) bool {
if _, ok := that.(accept); ok {
return true
}
return false
}
func (this reduce) Equal(that action) bool {
that1, ok := that.(reduce)
if !ok {
return false
}
return this == that1
}
func (this shift) Equal(that action) bool {
that1, ok := that.(shift)
if !ok {
return false
}
return this == that1
}
func (this accept) String() string { return "accept(0)" }
func (this shift) String() string { return fmt.Sprintf("shift:%d", this) }
func (this reduce) String() string {
return fmt.Sprintf("reduce:%d(%s)", this, productionsTable[this].String)
}

View File

@@ -0,0 +1,599 @@
// Code generated by gocc; DO NOT EDIT.
package parser
type (
actionTable [numStates]actionRow
actionRow struct {
canRecover bool
actions [numSymbols]action
}
)
var actionTab = actionTable{
actionRow{ // S0
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
nil, // string
nil, // number
nil, // name
shift(4), // (
nil, // )
nil, // .
},
},
actionRow{ // S1
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
accept(true), // ␚
nil, // string
nil, // number
nil, // name
nil, // (
nil, // )
nil, // .
},
},
actionRow{ // S2
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
reduce(1), // ␚, reduce: Schema
nil, // string
nil, // number
nil, // name
shift(4), // (
nil, // )
nil, // .
},
},
actionRow{ // S3
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
reduce(2), // ␚, reduce: ExprList
nil, // string
nil, // number
nil, // name
reduce(2), // (, reduce: ExprList
nil, // )
nil, // .
},
},
actionRow{ // S4
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
nil, // string
nil, // number
shift(6), // name
nil, // (
nil, // )
shift(7), // .
},
},
actionRow{ // S5
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
reduce(3), // ␚, reduce: ExprList
nil, // string
nil, // number
nil, // name
reduce(3), // (, reduce: ExprList
nil, // )
nil, // .
},
},
actionRow{ // S6
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
shift(10), // string
shift(11), // number
shift(12), // name
shift(13), // (
shift(14), // )
nil, // .
},
},
actionRow{ // S7
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
shift(10), // string
shift(11), // number
shift(12), // name
shift(13), // (
nil, // )
nil, // .
},
},
actionRow{ // S8
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
reduce(9), // string, reduce: Val
reduce(9), // number, reduce: Val
reduce(9), // name, reduce: Val
reduce(9), // (, reduce: Val
reduce(9), // ), reduce: Val
nil, // .
},
},
actionRow{ // S9
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
shift(19), // string
shift(20), // number
shift(21), // name
shift(22), // (
shift(23), // )
nil, // .
},
},
actionRow{ // S10
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
reduce(6), // string, reduce: Val
reduce(6), // number, reduce: Val
reduce(6), // name, reduce: Val
reduce(6), // (, reduce: Val
reduce(6), // ), reduce: Val
nil, // .
},
},
actionRow{ // S11
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
reduce(7), // string, reduce: Val
reduce(7), // number, reduce: Val
reduce(7), // name, reduce: Val
reduce(7), // (, reduce: Val
reduce(7), // ), reduce: Val
nil, // .
},
},
actionRow{ // S12
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
reduce(8), // string, reduce: Val
reduce(8), // number, reduce: Val
reduce(8), // name, reduce: Val
reduce(8), // (, reduce: Val
reduce(8), // ), reduce: Val
nil, // .
},
},
actionRow{ // S13
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
nil, // string
nil, // number
shift(24), // name
nil, // (
nil, // )
shift(25), // .
},
},
actionRow{ // S14
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
reduce(12), // ␚, reduce: Expr
nil, // string
nil, // number
nil, // name
reduce(12), // (, reduce: Expr
nil, // )
nil, // .
},
},
actionRow{ // S15
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
shift(10), // string
shift(11), // number
shift(12), // name
shift(13), // (
shift(27), // )
nil, // .
},
},
actionRow{ // S16
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
reduce(4), // string, reduce: ValList
reduce(4), // number, reduce: ValList
reduce(4), // name, reduce: ValList
reduce(4), // (, reduce: ValList
reduce(4), // ), reduce: ValList
nil, // .
},
},
actionRow{ // S17
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
nil, // string
nil, // number
nil, // name
nil, // (
reduce(9), // ), reduce: Val
nil, // .
},
},
actionRow{ // S18
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
nil, // string
nil, // number
nil, // name
nil, // (
shift(28), // )
nil, // .
},
},
actionRow{ // S19
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
nil, // string
nil, // number
nil, // name
nil, // (
reduce(6), // ), reduce: Val
nil, // .
},
},
actionRow{ // S20
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
nil, // string
nil, // number
nil, // name
nil, // (
reduce(7), // ), reduce: Val
nil, // .
},
},
actionRow{ // S21
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
nil, // string
nil, // number
nil, // name
nil, // (
reduce(8), // ), reduce: Val
nil, // .
},
},
actionRow{ // S22
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
nil, // string
nil, // number
shift(29), // name
nil, // (
nil, // )
shift(30), // .
},
},
actionRow{ // S23
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
reduce(11), // ␚, reduce: Expr
nil, // string
nil, // number
nil, // name
reduce(11), // (, reduce: Expr
nil, // )
nil, // .
},
},
actionRow{ // S24
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
shift(10), // string
shift(11), // number
shift(12), // name
shift(13), // (
shift(32), // )
nil, // .
},
},
actionRow{ // S25
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
shift(10), // string
shift(11), // number
shift(12), // name
shift(13), // (
nil, // )
nil, // .
},
},
actionRow{ // S26
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
reduce(5), // string, reduce: ValList
reduce(5), // number, reduce: ValList
reduce(5), // name, reduce: ValList
reduce(5), // (, reduce: ValList
reduce(5), // ), reduce: ValList
nil, // .
},
},
actionRow{ // S27
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
reduce(13), // ␚, reduce: Expr
nil, // string
nil, // number
nil, // name
reduce(13), // (, reduce: Expr
nil, // )
nil, // .
},
},
actionRow{ // S28
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
reduce(10), // ␚, reduce: Expr
nil, // string
nil, // number
nil, // name
reduce(10), // (, reduce: Expr
nil, // )
nil, // .
},
},
actionRow{ // S29
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
shift(10), // string
shift(11), // number
shift(12), // name
shift(13), // (
shift(35), // )
nil, // .
},
},
actionRow{ // S30
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
shift(10), // string
shift(11), // number
shift(12), // name
shift(13), // (
nil, // )
nil, // .
},
},
actionRow{ // S31
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
shift(19), // string
shift(20), // number
shift(21), // name
shift(22), // (
shift(38), // )
nil, // .
},
},
actionRow{ // S32
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
reduce(12), // string, reduce: Expr
reduce(12), // number, reduce: Expr
reduce(12), // name, reduce: Expr
reduce(12), // (, reduce: Expr
reduce(12), // ), reduce: Expr
nil, // .
},
},
actionRow{ // S33
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
shift(10), // string
shift(11), // number
shift(12), // name
shift(13), // (
shift(39), // )
nil, // .
},
},
actionRow{ // S34
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
shift(19), // string
shift(20), // number
shift(21), // name
shift(22), // (
shift(41), // )
nil, // .
},
},
actionRow{ // S35
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
nil, // string
nil, // number
nil, // name
nil, // (
reduce(12), // ), reduce: Expr
nil, // .
},
},
actionRow{ // S36
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
shift(10), // string
shift(11), // number
shift(12), // name
shift(13), // (
shift(42), // )
nil, // .
},
},
actionRow{ // S37
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
nil, // string
nil, // number
nil, // name
nil, // (
shift(43), // )
nil, // .
},
},
actionRow{ // S38
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
reduce(11), // string, reduce: Expr
reduce(11), // number, reduce: Expr
reduce(11), // name, reduce: Expr
reduce(11), // (, reduce: Expr
reduce(11), // ), reduce: Expr
nil, // .
},
},
actionRow{ // S39
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
reduce(13), // string, reduce: Expr
reduce(13), // number, reduce: Expr
reduce(13), // name, reduce: Expr
reduce(13), // (, reduce: Expr
reduce(13), // ), reduce: Expr
nil, // .
},
},
actionRow{ // S40
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
nil, // string
nil, // number
nil, // name
nil, // (
shift(44), // )
nil, // .
},
},
actionRow{ // S41
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
nil, // string
nil, // number
nil, // name
nil, // (
reduce(11), // ), reduce: Expr
nil, // .
},
},
actionRow{ // S42
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
nil, // string
nil, // number
nil, // name
nil, // (
reduce(13), // ), reduce: Expr
nil, // .
},
},
actionRow{ // S43
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
reduce(10), // string, reduce: Expr
reduce(10), // number, reduce: Expr
reduce(10), // name, reduce: Expr
reduce(10), // (, reduce: Expr
reduce(10), // ), reduce: Expr
nil, // .
},
},
actionRow{ // S44
canRecover: false,
actions: [numSymbols]action{
nil, // INVALID
nil, // ␚
nil, // string
nil, // number
nil, // name
nil, // (
reduce(10), // ), reduce: Expr
nil, // .
},
},
}

7
schema/parser/context.go Normal file
View File

@@ -0,0 +1,7 @@
// Code generated by gocc; DO NOT EDIT.
package parser
// Parser-specific user-defined and entirely-optional context,
// accessible as '$Context' in SDT actions.
type Context interface{}

373
schema/parser/gototable.go Normal file
View File

@@ -0,0 +1,373 @@
// Code generated by gocc; DO NOT EDIT.
package parser
const numNTSymbols = 6
type (
gotoTable [numStates]gotoRow
gotoRow [numNTSymbols]int
)
var gotoTab = gotoTable{
gotoRow{ // S0
-1, // S'
1, // Schema
2, // ExprList
-1, // ValList
-1, // Val
3, // Expr
},
gotoRow{ // S1
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S2
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
5, // Expr
},
gotoRow{ // S3
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S4
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S5
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S6
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
9, // Val
8, // Expr
},
gotoRow{ // S7
-1, // S'
-1, // Schema
-1, // ExprList
15, // ValList
16, // Val
8, // Expr
},
gotoRow{ // S8
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S9
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
18, // Val
17, // Expr
},
gotoRow{ // S10
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S11
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S12
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S13
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S14
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S15
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
26, // Val
8, // Expr
},
gotoRow{ // S16
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S17
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S18
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S19
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S20
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S21
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S22
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S23
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S24
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
31, // Val
8, // Expr
},
gotoRow{ // S25
-1, // S'
-1, // Schema
-1, // ExprList
33, // ValList
16, // Val
8, // Expr
},
gotoRow{ // S26
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S27
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S28
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S29
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
34, // Val
8, // Expr
},
gotoRow{ // S30
-1, // S'
-1, // Schema
-1, // ExprList
36, // ValList
16, // Val
8, // Expr
},
gotoRow{ // S31
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
37, // Val
17, // Expr
},
gotoRow{ // S32
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S33
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
26, // Val
8, // Expr
},
gotoRow{ // S34
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
40, // Val
17, // Expr
},
gotoRow{ // S35
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S36
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
26, // Val
8, // Expr
},
gotoRow{ // S37
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S38
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S39
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S40
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S41
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S42
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S43
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
gotoRow{ // S44
-1, // S'
-1, // Schema
-1, // ExprList
-1, // ValList
-1, // Val
-1, // Expr
},
}

217
schema/parser/parser.go Normal file
View File

@@ -0,0 +1,217 @@
// Code generated by gocc; DO NOT EDIT.
package parser
import (
"fmt"
"strings"
parseError "azalea/schema/errors"
"azalea/schema/token"
)
const (
numProductions = 14
numStates = 45
numSymbols = 14
)
// Stack
type stack struct {
state []int
attrib []Attrib
}
const iNITIAL_STACK_SIZE = 100
func newStack() *stack {
return &stack{
state: make([]int, 0, iNITIAL_STACK_SIZE),
attrib: make([]Attrib, 0, iNITIAL_STACK_SIZE),
}
}
func (s *stack) reset() {
s.state = s.state[:0]
s.attrib = s.attrib[:0]
}
func (s *stack) push(state int, a Attrib) {
s.state = append(s.state, state)
s.attrib = append(s.attrib, a)
}
func (s *stack) top() int {
return s.state[len(s.state)-1]
}
func (s *stack) peek(pos int) int {
return s.state[pos]
}
func (s *stack) topIndex() int {
return len(s.state) - 1
}
func (s *stack) popN(items int) []Attrib {
lo, hi := len(s.state)-items, len(s.state)
attrib := s.attrib[lo:hi]
s.state = s.state[:lo]
s.attrib = s.attrib[:lo]
return attrib
}
func (s *stack) String() string {
w := new(strings.Builder)
fmt.Fprintf(w, "stack:\n")
for i, st := range s.state {
fmt.Fprintf(w, "\t%d: %d , ", i, st)
if s.attrib[i] == nil {
fmt.Fprintf(w, "nil")
} else {
switch attr := s.attrib[i].(type) {
case *token.Token:
fmt.Fprintf(w, "%s", attr.Lit)
default:
fmt.Fprintf(w, "%v", attr)
}
}
fmt.Fprintf(w, "\n")
}
return w.String()
}
// Parser
type Parser struct {
stack *stack
nextToken *token.Token
pos int
Context Context
}
type Scanner interface {
Scan() (tok *token.Token)
}
func NewParser() *Parser {
p := &Parser{stack: newStack()}
p.Reset()
return p
}
func (p *Parser) Reset() {
p.stack.reset()
p.stack.push(0, nil)
}
func (p *Parser) Error(err error, scanner Scanner) (recovered bool, errorAttrib *parseError.Error) {
errorAttrib = &parseError.Error{
Err: err,
ErrorToken: p.nextToken,
ErrorSymbols: p.popNonRecoveryStates(),
ExpectedTokens: make([]string, 0, 8),
}
for t, action := range actionTab[p.stack.top()].actions {
if action != nil {
errorAttrib.ExpectedTokens = append(errorAttrib.ExpectedTokens, token.TokMap.Id(token.Type(t)))
}
}
if action := actionTab[p.stack.top()].actions[token.TokMap.Type("error")]; action != nil {
p.stack.push(int(action.(shift)), errorAttrib) // action can only be shift
} else {
return
}
if action := actionTab[p.stack.top()].actions[p.nextToken.Type]; action != nil {
recovered = true
}
for !recovered && p.nextToken.Type != token.EOF {
p.nextToken = scanner.Scan()
if action := actionTab[p.stack.top()].actions[p.nextToken.Type]; action != nil {
recovered = true
}
}
return
}
func (p *Parser) popNonRecoveryStates() (removedAttribs []parseError.ErrorSymbol) {
if rs, ok := p.firstRecoveryState(); ok {
errorSymbols := p.stack.popN(p.stack.topIndex() - rs)
removedAttribs = make([]parseError.ErrorSymbol, len(errorSymbols))
for i, e := range errorSymbols {
removedAttribs[i] = e
}
} else {
removedAttribs = []parseError.ErrorSymbol{}
}
return
}
// recoveryState points to the highest state on the stack, which can recover
func (p *Parser) firstRecoveryState() (recoveryState int, canRecover bool) {
recoveryState, canRecover = p.stack.topIndex(), actionTab[p.stack.top()].canRecover
for recoveryState > 0 && !canRecover {
recoveryState--
canRecover = actionTab[p.stack.peek(recoveryState)].canRecover
}
return
}
func (p *Parser) newError(err error) error {
e := &parseError.Error{
Err: err,
StackTop: p.stack.top(),
ErrorToken: p.nextToken,
}
actRow := actionTab[p.stack.top()]
for i, t := range actRow.actions {
if t != nil {
e.ExpectedTokens = append(e.ExpectedTokens, token.TokMap.Id(token.Type(i)))
}
}
return e
}
func (p *Parser) Parse(scanner Scanner) (res interface{}, err error) {
p.Reset()
p.nextToken = scanner.Scan()
for acc := false; !acc; {
action := actionTab[p.stack.top()].actions[p.nextToken.Type]
if action == nil {
if recovered, errAttrib := p.Error(nil, scanner); !recovered {
p.nextToken = errAttrib.ErrorToken
return nil, p.newError(nil)
}
if action = actionTab[p.stack.top()].actions[p.nextToken.Type]; action == nil {
panic("Error recovery led to invalid action")
}
}
switch act := action.(type) {
case accept:
res = p.stack.popN(1)[0]
acc = true
case shift:
p.stack.push(int(act), p.nextToken)
p.nextToken = scanner.Scan()
case reduce:
prod := productionsTable[int(act)]
attrib, err := prod.ReduceFunc(p.stack.popN(prod.NumSymbols), p.Context)
if err != nil {
return nil, p.newError(err)
} else {
p.stack.push(gotoTab[p.stack.top()][prod.NTType], attrib)
}
default:
panic("unknown action: " + action.String())
}
}
return res, nil
}

View File

@@ -0,0 +1,160 @@
// Code generated by gocc; DO NOT EDIT.
package parser
type (
ProdTab [numProductions]ProdTabEntry
ProdTabEntry struct {
String string
Id string
NTType int
Index int
NumSymbols int
ReduceFunc func([]Attrib, interface{}) (Attrib, error)
}
Attrib interface {
}
)
var productionsTable = ProdTab{
ProdTabEntry{
String: `S' : Schema << >>`,
Id: "S'",
NTType: 0,
Index: 0,
NumSymbols: 1,
ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) {
return X[0], nil
},
},
ProdTabEntry{
String: `Schema : ExprList << >>`,
Id: "Schema",
NTType: 1,
Index: 1,
NumSymbols: 1,
ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) {
return X[0], nil
},
},
ProdTabEntry{
String: `ExprList : Expr << >>`,
Id: "ExprList",
NTType: 2,
Index: 2,
NumSymbols: 1,
ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) {
return X[0], nil
},
},
ProdTabEntry{
String: `ExprList : ExprList Expr << >>`,
Id: "ExprList",
NTType: 2,
Index: 3,
NumSymbols: 2,
ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) {
return X[0], nil
},
},
ProdTabEntry{
String: `ValList : Val << >>`,
Id: "ValList",
NTType: 3,
Index: 4,
NumSymbols: 1,
ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) {
return X[0], nil
},
},
ProdTabEntry{
String: `ValList : ValList Val << >>`,
Id: "ValList",
NTType: 3,
Index: 5,
NumSymbols: 2,
ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) {
return X[0], nil
},
},
ProdTabEntry{
String: `Val : string << >>`,
Id: "Val",
NTType: 4,
Index: 6,
NumSymbols: 1,
ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) {
return X[0], nil
},
},
ProdTabEntry{
String: `Val : number << >>`,
Id: "Val",
NTType: 4,
Index: 7,
NumSymbols: 1,
ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) {
return X[0], nil
},
},
ProdTabEntry{
String: `Val : name << >>`,
Id: "Val",
NTType: 4,
Index: 8,
NumSymbols: 1,
ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) {
return X[0], nil
},
},
ProdTabEntry{
String: `Val : Expr << >>`,
Id: "Val",
NTType: 4,
Index: 9,
NumSymbols: 1,
ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) {
return X[0], nil
},
},
ProdTabEntry{
String: `Expr : "(" name Val Val ")" << >>`,
Id: "Expr",
NTType: 5,
Index: 10,
NumSymbols: 5,
ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) {
return X[0], nil
},
},
ProdTabEntry{
String: `Expr : "(" name Val ")" << >>`,
Id: "Expr",
NTType: 5,
Index: 11,
NumSymbols: 4,
ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) {
return X[0], nil
},
},
ProdTabEntry{
String: `Expr : "(" name ")" << >>`,
Id: "Expr",
NTType: 5,
Index: 12,
NumSymbols: 3,
ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) {
return X[0], nil
},
},
ProdTabEntry{
String: `Expr : "(" "." ValList ")" << >>`,
Id: "Expr",
NTType: 5,
Index: 13,
NumSymbols: 4,
ReduceFunc: func(X []Attrib, C interface{}) (Attrib, error) {
return X[0], nil
},
},
}

View File

@@ -138,16 +138,22 @@ var TokMap = TokenMap{
typeMap: []string{ typeMap: []string{
"INVALID", "INVALID",
"␚", "␚",
"name",
"number",
"string", "string",
"number",
"name",
"(",
")",
".",
}, },
idMap: map[string]Type{ idMap: map[string]Type{
"INVALID": 0, "INVALID": 0,
"␚": 1, "␚": 1,
"name": 2, "string": 2,
"number": 3, "number": 3,
"string": 4, "name": 4,
"(": 5,
")": 6,
".": 7,
}, },
} }