-
Notifications
You must be signed in to change notification settings - Fork 15
/
types.go
116 lines (95 loc) · 2.29 KB
/
types.go
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
package fmr
import (
"encoding/gob"
"fmt"
"github.com/liuzl/dict"
"github.com/mitchellh/hashstructure"
)
func init() {
gob.Register(RbKey{})
}
// A Grammar stores a Context-Free Grammar
type Grammar struct {
Name string `json:"name"`
Rules map[string]*Rule `json:"rules"`
Frames map[string]*Rule `json:"frames"`
Regexps map[string]string `json:"regexps"`
Refined bool `json:"refined"`
trie *dict.Cedar
index map[string]*Index
ruleIndex map[string]*Index
includes []*Grammar
}
// An Index contains two sets for frames' names and rules' names
type Index struct {
Frames map[RbKey]struct{}
Rules map[RbKey]struct{}
}
// A RbKey identifies a specific RuleBody by name and id
type RbKey struct {
RuleName string `json:"rule_name"`
BodyID uint64 `json:"body_id"`
}
// A Pos specifies the start and end positions
type Pos struct {
StartByte int `json:"start_byte"`
EndByte int `json:"end_byte"`
}
// A Slot contains the Pos and its corresponding parse trees
type Slot struct {
Pos
Trees []*Node
}
// A Frame is a frame consists of Slots
type Frame struct {
Slots map[uint64][]*Slot
Complete bool
}
func (f *Frame) String() string {
return fmt.Sprintf("Complete:%+v, %+v", f.Complete, f.Slots)
}
// A Rule stores a set of production rules of Name
type Rule struct {
Name string `json:"-"`
Body map[uint64]*RuleBody `json:"body,omitempty"`
}
// A RuleBody is one production rule
type RuleBody struct {
Terms []*Term `json:"terms"`
F *FMR `json:"f,omitempty"`
}
// TermType of grammar terms
type TermType byte
//go:generate jsonenums -type=TermType
// definition of TermTypes
const (
EOF TermType = iota
Nonterminal
Terminal
Any
List
)
// A Term is the component of RuleBody
type Term struct {
Value string `json:"value"`
Type TermType `json:"type"`
Meta interface{} `json:"meta"`
}
// Key returns a unique key for Term t
func (t *Term) Key() uint64 {
hash, err := hashstructure.Hash(t, nil)
if err != nil {
return 0
}
return hash
}
// Arg is the type of argument for functions
type Arg struct {
Type string `json:"type"`
Value interface{} `json:"value"`
}
// FMR stands for Funtional Meaning Representation
type FMR struct {
Fn string `json:"fn,omitempty"`
Args []*Arg `json:"args,omitempty"`
}