File: builder.go

package info (click to toggle)
postgresql-16-age 1.6.0~rc0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,016 kB
  • sloc: ansic: 33,556; sql: 13,270; python: 2,530; yacc: 2,518; java: 1,418; lex: 1,325; perl: 294; sh: 286; makefile: 123; javascript: 24
file content (289 lines) | stat: -rw-r--r-- 7,877 bytes parent folder | download | duplicates (2)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package age

import (
	"fmt"
	"math"
	"math/big"
	"strconv"
	"strings"
	"github.com/antlr/antlr4/runtime/Go/antlr/v4"
	"github.com/apache/age/drivers/golang/parser"
)

const MaxUint = ^uint(0)
const MaxInt = int(MaxUint >> 1)
const MinUint = 0
const MinInt = -MaxInt - 1

type Unmarshaller interface {
	unmarshal(text string) (Entity, error)
}

type AGUnmarshaler struct {
	Unmarshaller
	ageParser   *parser.AgeParser
	visitor     parser.AgeVisitor
	errListener *AGErrorListener
	vcache      map[int64]interface{}
}

func NewAGUnmarshaler() *AGUnmarshaler {
	vcache := make(map[int64]interface{})

	m := &AGUnmarshaler{ageParser: parser.NewAgeParser(nil),
		visitor:     &UnmarshalVisitor{vcache: vcache},
		errListener: NewAGErrorListener(),
		vcache:      vcache,
	}
	m.ageParser.AddErrorListener(m.errListener)

	return m
}

func (p *AGUnmarshaler) unmarshal(text string) (Entity, error) {
	if len(text) == 0 {
		return NewSimpleEntity(nil), nil
	}
	input := antlr.NewInputStream(text)
	lexer := parser.NewAgeLexer(input)
	stream := antlr.NewCommonTokenStream(lexer, 0)
	p.ageParser.SetInputStream(stream)
	tree := p.ageParser.Ageout()
	rst := tree.Accept(p.visitor)

	if len(p.errListener.errList) > 0 {
		var ape *AgeParseError = nil
		errs := make([]string, len(p.errListener.errList))
		for idx, re := range p.errListener.errList {
			errs[idx] = re.GetMessage()
			fmt.Println(re)
		}
		p.errListener.clearErrs()

		ape = &AgeParseError{msg: "Cypher query:" + text, errors: errs}

		return nil, ape
	}

	if !IsEntity(rst) {
		rst = NewSimpleEntity(rst)
	}

	return rst.(Entity), nil
}

type AGErrorListener struct {
	*antlr.DefaultErrorListener
	errList []antlr.RecognitionException
}

func NewAGErrorListener() *AGErrorListener {
	return &AGErrorListener{DefaultErrorListener: &antlr.DefaultErrorListener{}, errList: []antlr.RecognitionException{}}
}

func (el *AGErrorListener) SyntaxError(recognizer antlr.Recognizer, offendingSymbol interface{}, line, column int, msg string, e antlr.RecognitionException) {
	el.errList = append(el.errList, e)
}

func (el *AGErrorListener) getErrs() []antlr.RecognitionException {
	return el.errList
}
func (el *AGErrorListener) clearErrs() {
	el.errList = []antlr.RecognitionException{}
}

type UnmarshalVisitor struct {
	parser.AgeVisitor
	vcache map[int64]interface{}
}

func (v *UnmarshalVisitor) Visit(tree antlr.ParseTree) interface{} { return nil }
func (v *UnmarshalVisitor) VisitChildren(node antlr.RuleNode) interface{} {
	var rtn interface{}
	for _, c := range node.GetChildren() {
		pt := c.(antlr.ParseTree)
		rtn = pt.Accept(v)
	}
	return rtn
}
func (v *UnmarshalVisitor) VisitTerminal(node antlr.TerminalNode) interface{} { return nil }
func (v *UnmarshalVisitor) VisitErrorNode(node antlr.ErrorNode) interface{}   { return nil }
func (v *UnmarshalVisitor) VisitAgeout(ctx *parser.AgeoutContext) interface{} {
	rtn := v.VisitChildren(ctx)
	return rtn
}

// Visit a parse tree produced by AgeParser#vertex.
func (v *UnmarshalVisitor) VisitVertex(ctx *parser.VertexContext) interface{} {
	propCtx := ctx.Properties()
	props := propCtx.Accept(v).(map[string]interface{})
	// fmt.Println(" * VisitVertex:", props)
	vid := int64(props["id"].(int64))
	vertex, ok := v.vcache[vid]

	if !ok {
		vertex = NewVertex(vid, props["label"].(string), props["properties"].(map[string]interface{}))
		v.vcache[vid] = vertex
	}

	return vertex
}

// Visit a parse tree produced by AgeParser#edge.
func (v *UnmarshalVisitor) VisitEdge(ctx *parser.EdgeContext) interface{} {
	propCtx := ctx.Properties()
	props := propCtx.Accept(v).(map[string]interface{})
	// fmt.Println(" * VisitEdge:", props)

	edge := NewEdge(int64(props["id"].(int64)), props["label"].(string),
		int64(props["start_id"].(int64)), int64(props["end_id"].(int64)),
		props["properties"].(map[string]interface{}))

	return edge
}

// Visit a parse tree produced by AgeParser#path.
func (v *UnmarshalVisitor) VisitPath(ctx *parser.PathContext) interface{} {
	entities := []Entity{}

	for _, child := range ctx.GetChildren() {
		switch child.(type) {
		case *parser.VertexContext:
			v := child.(*parser.VertexContext).Accept(v)
			// fmt.Println(v)
			entities = append(entities, v.(Entity))
		case *parser.EdgeContext:
			e := child.(*parser.EdgeContext).Accept(v)
			// fmt.Println(e)
			entities = append(entities, e.(Entity))
		default:
		}
	}

	path := NewPath(entities)
	return path
}

// Visit a parse tree produced by AgeParser#value.
func (v *UnmarshalVisitor) VisitValue(ctx *parser.ValueContext) interface{} {
	child := ctx.GetChild(0)
	switch child.(type) {
	case *antlr.TerminalNodeImpl:
		rtn, err := unmarshalTerm(child.(*antlr.TerminalNodeImpl))
		if err != nil {
			panic(err)
		}
		return rtn
	default:
		return child.(antlr.ParserRuleContext).Accept(v)
	}
}

// Visit a parse tree produced by AgeParser#properties.
func (v *UnmarshalVisitor) VisitProperties(ctx *parser.PropertiesContext) interface{} {
	props := make(map[string]interface{})
	for _, pairCtx := range ctx.AllPair() {
		pairCtx.Accept(v)
		pair := pairCtx.(*parser.PairContext)
		key := strings.Trim(pair.STRING().GetText(), "\"")
		// fmt.Println("Pair KEY:", key)
		value := pair.Value().Accept(v)
		props[key] = value
	}

	return props
}

// Visit a parse tree produced by AgeParser#pair.
func (v *UnmarshalVisitor) VisitPair(ctx *parser.PairContext) interface{} {
	return nil
}

// Visit a parse tree produced by AgeParser#arr.
func (v *UnmarshalVisitor) VisitArr(ctx *parser.ArrContext) interface{} {
	var arr []interface{}
	for _, child := range ctx.GetChildren() {
		switch child.(type) {
		case *antlr.TerminalNodeImpl:
			// skip
			break
		default:
			el := child.(antlr.ParserRuleContext).Accept(v)
			arr = append(arr, el)
		}
	}
	return arr
}

func unmarshalTerm(ctx *antlr.TerminalNodeImpl) (interface{}, error) {
	txt := ctx.GetText()
	switch ctx.GetSymbol().GetTokenType() {
	case parser.AgeLexerSTRING:
		return strings.Trim(txt, "\""), nil
	case parser.AgeLexerNUMERIC:
		numStr := txt[:len(txt)-9]
		// fmt.Println("txt   ", txt)
		// fmt.Println("numStr", numStr)
		if strings.Contains(numStr, ".") {
			bi := new(big.Float)
			bi, ok := bi.SetString(numStr)
			if !ok {
				return nil, &AgeParseError{msg: "Parse big float " + txt}
			}
			return bi, nil
		} else {
			bi := new(big.Int)
			bi, ok := bi.SetString(numStr, 10)
			if !ok {
				return nil, &AgeParseError{msg: "Parse big int " + txt}
			}
			return bi, nil
		}
	case parser.AgeLexerNUMBER:
		if strings.Contains(txt, ".") {
			return strconv.ParseFloat(txt, 64)
		} else {
			return strconv.ParseInt(txt, 10, 64)
		}
	case parser.AgeLexerFLOAT_EXPR:
		switch txt {
		case "NaN":
			return math.NaN(), nil
		case "-Infinity":
			return math.Inf(-1), nil
		case "Infinity":
			return math.Inf(1), nil
		default:
			return nil, &AgeParseError{msg: "Unknown float expression" + txt}
		}
	case parser.AgeLexerBOOL:
		s, err := strconv.ParseBool(txt)
		if err != nil {
			return nil, err
		} else {
			return s, nil
		}
	case parser.AgeLexerNULL:
		return nil, nil
	default:
		return nil, nil
	}
}