-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
62 lines (50 loc) · 1.34 KB
/
parser.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
// Open source implementation of the SOQL parser.
package parser
import (
"errors"
"strconv"
"time"
"github.com/shellyln/go-open-soql-parser/soql/parser/core"
"github.com/shellyln/go-open-soql-parser/soql/parser/postprocess"
"github.com/shellyln/go-open-soql-parser/soql/parser/types"
. "github.com/shellyln/takenoco/base"
. "github.com/shellyln/takenoco/string"
)
var (
queryParser ParserFn
)
func init() {
queryParser = core.Query()
}
func Parse(s string) (*types.SoqlQuery, error) {
meta := &types.SoqlQueryMeta{
Version: "0.9",
Date: time.Now().UTC(),
Source: s,
}
out, err := queryParser(*NewStringParserContext(s))
if err != nil {
pos := GetLineAndColPosition(s, out.SourcePosition, 4)
return nil, errors.New(
err.Error() +
"\n --> Line " + strconv.Itoa(pos.Line) +
", Col " + strconv.Itoa(pos.Col) + "\n" +
pos.ErrSource)
}
if out.MatchStatus != MatchStatus_Matched {
pos := GetLineAndColPosition(s, out.SourcePosition, 4)
return nil, errors.New(
"Parse failed" +
"\n --> Line " + strconv.Itoa(pos.Line) +
", Col " + strconv.Itoa(pos.Col) + "\n" +
pos.ErrSource)
}
q := out.AstStack[0].Value.(types.SoqlQuery)
q.Meta = meta
if err := postprocess.Normalize(&q); err != nil {
return nil, err
}
endDate := time.Now()
q.Meta.ElapsedTime = endDate.Sub(q.Meta.Date)
return &q, nil
}