diff options
author | wmin0 <wmin0@cobinhood.com> | 2019-01-17 15:51:56 +0800 |
---|---|---|
committer | Jhih-Ming Huang <jm.huang@cobinhood.com> | 2019-03-26 17:48:21 +0800 |
commit | 56c1798ede9eedfffc8d1f7646084db63ca97466 (patch) | |
tree | c9d7eda4be7ecbf7af9484e628d493f705f1a721 | |
parent | 1ea57eed7e9e37b40d27b11fe316cfe3569fade6 (diff) | |
download | dexon-56c1798ede9eedfffc8d1f7646084db63ca97466.tar.gz dexon-56c1798ede9eedfffc8d1f7646084db63ca97466.tar.zst dexon-56c1798ede9eedfffc8d1f7646084db63ca97466.zip |
core: vm: sqlvm: add sql language parser prototyping
Add sql language parser prototyping along with
1. grammar file implemented with github.com/mna/pigeon
2. ast node struct definition
3. simple parser test which only test if error existed
4. ast printer utility for visualizing parsing result
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | core/vm/sqlvm/cmd/ast-printer/main.go | 16 | ||||
-rw-r--r-- | core/vm/sqlvm/grammar.go | 7505 | ||||
-rw-r--r-- | core/vm/sqlvm/grammar.peg | 763 | ||||
-rw-r--r-- | core/vm/sqlvm/parser.go | 125 | ||||
-rw-r--r-- | core/vm/sqlvm/parser_test.go | 82 | ||||
-rw-r--r-- | core/vm/sqlvm/type.go | 408 | ||||
-rw-r--r-- | vendor/github.com/shopspring/decimal/LICENSE | 45 | ||||
-rw-r--r-- | vendor/github.com/shopspring/decimal/README.md | 126 | ||||
-rw-r--r-- | vendor/github.com/shopspring/decimal/decimal-go.go | 414 | ||||
-rw-r--r-- | vendor/github.com/shopspring/decimal/decimal.go | 1434 | ||||
-rw-r--r-- | vendor/github.com/shopspring/decimal/rounding.go | 118 | ||||
-rw-r--r-- | vendor/vendor.json | 6 |
13 files changed, 11043 insertions, 0 deletions
@@ -57,6 +57,7 @@ devtools: env GOBIN= go get -u github.com/kevinburke/go-bindata/go-bindata env GOBIN= go get -u github.com/fjl/gencodec env GOBIN= go get -u github.com/golang/protobuf/protoc-gen-go + env GOBIN= go get -u github.com/mna/pigeon env GOBIN= go install ./cmd/abigen @type "npm" 2> /dev/null || echo 'Please install node.js and npm' @type "solc" 2> /dev/null || echo 'Please install solc' diff --git a/core/vm/sqlvm/cmd/ast-printer/main.go b/core/vm/sqlvm/cmd/ast-printer/main.go new file mode 100644 index 000000000..ad10a54ce --- /dev/null +++ b/core/vm/sqlvm/cmd/ast-printer/main.go @@ -0,0 +1,16 @@ +package main + +import ( + "fmt" + "os" + + "github.com/dexon-foundation/dexon/core/vm/sqlvm" +) + +func main() { + n, err := sqlvm.ParseString(os.Args[1]) + fmt.Printf("err: %+v\n", err) + if err == nil { + sqlvm.PrintAST(n, "") + } +} diff --git a/core/vm/sqlvm/grammar.go b/core/vm/sqlvm/grammar.go new file mode 100644 index 000000000..dad07b1eb --- /dev/null +++ b/core/vm/sqlvm/grammar.go @@ -0,0 +1,7505 @@ +// Code generated by pigeon; DO NOT EDIT. + +package sqlvm + +import ( + "bytes" + "errors" + "fmt" + "io" + "io/ioutil" + "math" + "os" + "sort" + "strconv" + "strings" + "unicode" + "unicode/utf8" +) + +var g = &grammar{ + rules: []*rule{ + { + name: "S", + pos: position{line: 5, col: 1, offset: 19}, + expr: &actionExpr{ + pos: position{line: 6, col: 5, offset: 25}, + run: (*parser).callonS1, + expr: &seqExpr{ + pos: position{line: 6, col: 5, offset: 25}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 6, col: 5, offset: 25}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 6, col: 7, offset: 27}, + label: "x", + expr: &zeroOrOneExpr{ + pos: position{line: 6, col: 9, offset: 29}, + expr: &ruleRefExpr{ + pos: position{line: 6, col: 9, offset: 29}, + name: "Stmt", + }, + }, + }, + &ruleRefExpr{ + pos: position{line: 6, col: 15, offset: 35}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 6, col: 17, offset: 37}, + label: "xs", + expr: &zeroOrMoreExpr{ + pos: position{line: 6, col: 20, offset: 40}, + expr: &actionExpr{ + pos: position{line: 6, col: 22, offset: 42}, + run: (*parser).callonS10, + expr: &seqExpr{ + pos: position{line: 6, col: 22, offset: 42}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 6, col: 22, offset: 42}, + val: ";", + ignoreCase: false, + }, + &ruleRefExpr{ + pos: position{line: 6, col: 26, offset: 46}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 6, col: 28, offset: 48}, + label: "s", + expr: &zeroOrOneExpr{ + pos: position{line: 6, col: 30, offset: 50}, + expr: &ruleRefExpr{ + pos: position{line: 6, col: 30, offset: 50}, + name: "Stmt", + }, + }, + }, + &ruleRefExpr{ + pos: position{line: 6, col: 36, offset: 56}, + name: "_", + }, + }, + }, + }, + }, + }, + &ruleRefExpr{ + pos: position{line: 6, col: 59, offset: 79}, + name: "EOF", + }, + }, + }, + }, + }, + { + name: "Stmt", + pos: position{line: 10, col: 1, offset: 131}, + expr: &choiceExpr{ + pos: position{line: 11, col: 4, offset: 139}, + alternatives: []interface{}{ + &ruleRefExpr{ + pos: position{line: 11, col: 4, offset: 139}, + name: "SelectStmt", + }, + &ruleRefExpr{ + pos: position{line: 12, col: 4, offset: 153}, + name: "UpdateStmt", + }, + &ruleRefExpr{ + pos: position{line: 13, col: 4, offset: 167}, + name: "DeleteStmt", + }, + &ruleRefExpr{ + pos: position{line: 14, col: 4, offset: 181}, + name: "InsertStmt", + }, + &ruleRefExpr{ + pos: position{line: 15, col: 4, offset: 195}, + name: "CreateTableStmt", + }, + &ruleRefExpr{ + pos: position{line: 16, col: 4, offset: 214}, + name: "CreateIndexStmt", + }, + }, + }, + }, + { + name: "SelectStmt", + pos: position{line: 18, col: 1, offset: 231}, + expr: &actionExpr{ + pos: position{line: 19, col: 4, offset: 245}, + run: (*parser).callonSelectStmt1, + expr: &seqExpr{ + pos: position{line: 19, col: 4, offset: 245}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 19, col: 4, offset: 245}, + name: "SelectToken", + }, + &ruleRefExpr{ + pos: position{line: 20, col: 2, offset: 258}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 20, col: 4, offset: 260}, + label: "f", + expr: &ruleRefExpr{ + pos: position{line: 20, col: 6, offset: 262}, + name: "SelectColumn", + }, + }, + &labeledExpr{ + pos: position{line: 20, col: 19, offset: 275}, + label: "fs", + expr: &zeroOrMoreExpr{ + pos: position{line: 20, col: 22, offset: 278}, + expr: &actionExpr{ + pos: position{line: 20, col: 24, offset: 280}, + run: (*parser).callonSelectStmt9, + expr: &seqExpr{ + pos: position{line: 20, col: 24, offset: 280}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 20, col: 24, offset: 280}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 20, col: 26, offset: 282}, + name: "SeparatorToken", + }, + &ruleRefExpr{ + pos: position{line: 20, col: 41, offset: 297}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 20, col: 43, offset: 299}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 20, col: 45, offset: 301}, + name: "SelectColumn", + }, + }, + }, + }, + }, + }, + }, + &labeledExpr{ + pos: position{line: 21, col: 2, offset: 336}, + label: "table", + expr: &zeroOrOneExpr{ + pos: position{line: 21, col: 8, offset: 342}, + expr: &actionExpr{ + pos: position{line: 21, col: 10, offset: 344}, + run: (*parser).callonSelectStmt18, + expr: &seqExpr{ + pos: position{line: 21, col: 10, offset: 344}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 21, col: 10, offset: 344}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 21, col: 12, offset: 346}, + name: "FromToken", + }, + &ruleRefExpr{ + pos: position{line: 21, col: 22, offset: 356}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 21, col: 24, offset: 358}, + label: "i", + expr: &ruleRefExpr{ + pos: position{line: 21, col: 26, offset: 360}, + name: "Identifier", + }, + }, + }, + }, + }, + }, + }, + &labeledExpr{ + pos: position{line: 22, col: 2, offset: 393}, + label: "where", + expr: &zeroOrOneExpr{ + pos: position{line: 22, col: 8, offset: 399}, + expr: &actionExpr{ + pos: position{line: 22, col: 10, offset: 401}, + run: (*parser).callonSelectStmt27, + expr: &seqExpr{ + pos: position{line: 22, col: 10, offset: 401}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 22, col: 10, offset: 401}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 22, col: 12, offset: 403}, + label: "w", + expr: &ruleRefExpr{ + pos: position{line: 22, col: 14, offset: 405}, + name: "WhereClause", + }, + }, + }, + }, + }, + }, + }, + &labeledExpr{ + pos: position{line: 23, col: 2, offset: 439}, + label: "group", + expr: &zeroOrOneExpr{ + pos: position{line: 23, col: 8, offset: 445}, + expr: &actionExpr{ + pos: position{line: 23, col: 10, offset: 447}, + run: (*parser).callonSelectStmt34, + expr: &seqExpr{ + pos: position{line: 23, col: 10, offset: 447}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 23, col: 10, offset: 447}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 23, col: 12, offset: 449}, + label: "g", + expr: &ruleRefExpr{ + pos: position{line: 23, col: 14, offset: 451}, + name: "GroupByClause", + }, + }, + }, + }, + }, + }, + }, + &labeledExpr{ + pos: position{line: 24, col: 2, offset: 487}, + label: "order", + expr: &zeroOrOneExpr{ + pos: position{line: 24, col: 8, offset: 493}, + expr: &actionExpr{ + pos: position{line: 24, col: 10, offset: 495}, + run: (*parser).callonSelectStmt41, + expr: &seqExpr{ + pos: position{line: 24, col: 10, offset: 495}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 24, col: 10, offset: 495}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 24, col: 12, offset: 497}, + label: "or", + expr: &ruleRefExpr{ + pos: position{line: 24, col: 15, offset: 500}, + name: "OrderByClause", + }, + }, + }, + }, + }, + }, + }, + &labeledExpr{ + pos: position{line: 25, col: 2, offset: 537}, + label: "limit", + expr: &zeroOrOneExpr{ + pos: position{line: 25, col: 8, offset: 543}, + expr: &actionExpr{ + pos: position{line: 25, col: 10, offset: 545}, + run: (*parser).callonSelectStmt48, + expr: &seqExpr{ + pos: position{line: 25, col: 10, offset: 545}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 25, col: 10, offset: 545}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 25, col: 12, offset: 547}, + label: "l", + expr: &ruleRefExpr{ + pos: position{line: 25, col: 14, offset: 549}, + name: "LimitClause", + }, + }, + }, + }, + }, + }, + }, + &labeledExpr{ + pos: position{line: 26, col: 2, offset: 583}, + label: "offset", + expr: &zeroOrOneExpr{ + pos: position{line: 26, col: 9, offset: 590}, + expr: &actionExpr{ + pos: position{line: 26, col: 11, offset: 592}, + run: (*parser).callonSelectStmt55, + expr: &seqExpr{ + pos: position{line: 26, col: 11, offset: 592}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 26, col: 11, offset: 592}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 26, col: 13, offset: 594}, + label: "of", + expr: &ruleRefExpr{ + pos: position{line: 26, col: 16, offset: 597}, + name: "OffsetClause", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "SelectColumn", + pos: position{line: 61, col: 1, offset: 1247}, + expr: &choiceExpr{ + pos: position{line: 62, col: 4, offset: 1263}, + alternatives: []interface{}{ + &ruleRefExpr{ + pos: position{line: 62, col: 4, offset: 1263}, + name: "AnyLiteral", + }, + &ruleRefExpr{ + pos: position{line: 63, col: 4, offset: 1277}, + name: "Expr", + }, + }, + }, + }, + { + name: "UpdateStmt", + pos: position{line: 65, col: 1, offset: 1283}, + expr: &actionExpr{ + pos: position{line: 66, col: 4, offset: 1297}, + run: (*parser).callonUpdateStmt1, + expr: &seqExpr{ + pos: position{line: 66, col: 4, offset: 1297}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 66, col: 4, offset: 1297}, + name: "UpdateToken", + }, + &ruleRefExpr{ + pos: position{line: 67, col: 2, offset: 1310}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 67, col: 4, offset: 1312}, + label: "table", + expr: &ruleRefExpr{ + pos: position{line: 67, col: 10, offset: 1318}, + name: "Identifier", + }, + }, + &ruleRefExpr{ + pos: position{line: 68, col: 2, offset: 1330}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 68, col: 4, offset: 1332}, + name: "SetToken", + }, + &ruleRefExpr{ + pos: position{line: 69, col: 2, offset: 1342}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 69, col: 4, offset: 1344}, + label: "a", + expr: &ruleRefExpr{ + pos: position{line: 69, col: 6, offset: 1346}, + name: "Assignment", + }, + }, + &labeledExpr{ + pos: position{line: 69, col: 17, offset: 1357}, + label: "as", + expr: &zeroOrMoreExpr{ + pos: position{line: 69, col: 20, offset: 1360}, + expr: &actionExpr{ + pos: position{line: 69, col: 22, offset: 1362}, + run: (*parser).callonUpdateStmt14, + expr: &seqExpr{ + pos: position{line: 69, col: 22, offset: 1362}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 69, col: 22, offset: 1362}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 69, col: 24, offset: 1364}, + name: "SeparatorToken", + }, + &ruleRefExpr{ + pos: position{line: 69, col: 39, offset: 1379}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 69, col: 41, offset: 1381}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 69, col: 43, offset: 1383}, + name: "Assignment", + }, + }, + }, + }, + }, + }, + }, + &labeledExpr{ + pos: position{line: 70, col: 2, offset: 1416}, + label: "where", + expr: &zeroOrOneExpr{ + pos: position{line: 70, col: 8, offset: 1422}, + expr: &actionExpr{ + pos: position{line: 70, col: 10, offset: 1424}, + run: (*parser).callonUpdateStmt23, + expr: &seqExpr{ + pos: position{line: 70, col: 10, offset: 1424}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 70, col: 10, offset: 1424}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 70, col: 12, offset: 1426}, + label: "w", + expr: &ruleRefExpr{ + pos: position{line: 70, col: 14, offset: 1428}, + name: "WhereClause", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "DeleteStmt", + pos: position{line: 86, col: 1, offset: 1700}, + expr: &actionExpr{ + pos: position{line: 87, col: 4, offset: 1714}, + run: (*parser).callonDeleteStmt1, + expr: &seqExpr{ + pos: position{line: 87, col: 4, offset: 1714}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 87, col: 4, offset: 1714}, + name: "DeleteToken", + }, + &ruleRefExpr{ + pos: position{line: 88, col: 2, offset: 1727}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 88, col: 4, offset: 1729}, + name: "FromToken", + }, + &ruleRefExpr{ + pos: position{line: 89, col: 2, offset: 1740}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 89, col: 4, offset: 1742}, + label: "table", + expr: &ruleRefExpr{ + pos: position{line: 89, col: 10, offset: 1748}, + name: "Identifier", + }, + }, + &labeledExpr{ + pos: position{line: 90, col: 2, offset: 1760}, + label: "where", + expr: &zeroOrOneExpr{ + pos: position{line: 90, col: 8, offset: 1766}, + expr: &actionExpr{ + pos: position{line: 90, col: 10, offset: 1768}, + run: (*parser).callonDeleteStmt11, + expr: &seqExpr{ + pos: position{line: 90, col: 10, offset: 1768}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 90, col: 10, offset: 1768}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 90, col: 12, offset: 1770}, + label: "w", + expr: &ruleRefExpr{ + pos: position{line: 90, col: 14, offset: 1772}, + name: "WhereClause", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "InsertStmt", + pos: position{line: 105, col: 1, offset: 2004}, + expr: &actionExpr{ + pos: position{line: 106, col: 4, offset: 2018}, + run: (*parser).callonInsertStmt1, + expr: &seqExpr{ + pos: position{line: 106, col: 4, offset: 2018}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 106, col: 4, offset: 2018}, + name: "InsertToken", + }, + &ruleRefExpr{ + pos: position{line: 107, col: 2, offset: 2031}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 107, col: 4, offset: 2033}, + name: "IntoToken", + }, + &ruleRefExpr{ + pos: position{line: 108, col: 2, offset: 2044}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 108, col: 4, offset: 2046}, + label: "table", + expr: &ruleRefExpr{ + pos: position{line: 108, col: 10, offset: 2052}, + name: "Identifier", + }, + }, + &ruleRefExpr{ + pos: position{line: 109, col: 2, offset: 2064}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 109, col: 4, offset: 2066}, + label: "insert", + expr: &choiceExpr{ + pos: position{line: 109, col: 13, offset: 2075}, + alternatives: []interface{}{ + &ruleRefExpr{ + pos: position{line: 109, col: 13, offset: 2075}, + name: "InsertWithColumnClause", + }, + &ruleRefExpr{ + pos: position{line: 109, col: 38, offset: 2100}, + name: "InsertWithDefaultClause", + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "InsertValue", + pos: position{line: 117, col: 1, offset: 2215}, + expr: &actionExpr{ + pos: position{line: 118, col: 4, offset: 2230}, + run: (*parser).callonInsertValue1, + expr: &seqExpr{ + pos: position{line: 118, col: 4, offset: 2230}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 118, col: 4, offset: 2230}, + val: "(", + ignoreCase: false, + }, + &ruleRefExpr{ + pos: position{line: 118, col: 8, offset: 2234}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 118, col: 10, offset: 2236}, + label: "e", + expr: &ruleRefExpr{ + pos: position{line: 118, col: 12, offset: 2238}, + name: "MultiExprWithDefault", + }, + }, + &ruleRefExpr{ + pos: position{line: 118, col: 33, offset: 2259}, + name: "_", + }, + &litMatcher{ + pos: position{line: 118, col: 35, offset: 2261}, + val: ")", + ignoreCase: false, + }, + }, + }, + }, + }, + { + name: "CreateTableStmt", + pos: position{line: 121, col: 1, offset: 2284}, + expr: &actionExpr{ + pos: position{line: 122, col: 4, offset: 2303}, + run: (*parser).callonCreateTableStmt1, + expr: &seqExpr{ + pos: position{line: 122, col: 4, offset: 2303}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 122, col: 4, offset: 2303}, + name: "CreateToken", + }, + &ruleRefExpr{ + pos: position{line: 123, col: 2, offset: 2316}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 123, col: 4, offset: 2318}, + name: "TableToken", + }, + &ruleRefExpr{ + pos: position{line: 124, col: 2, offset: 2330}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 124, col: 4, offset: 2332}, + label: "table", + expr: &ruleRefExpr{ + pos: position{line: 124, col: 10, offset: 2338}, + name: "Identifier", + }, + }, + &ruleRefExpr{ + pos: position{line: 125, col: 2, offset: 2350}, + name: "_", + }, + &litMatcher{ + pos: position{line: 125, col: 4, offset: 2352}, + val: "(", + ignoreCase: false, + }, + &ruleRefExpr{ + pos: position{line: 126, col: 2, offset: 2357}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 126, col: 4, offset: 2359}, + label: "column", + expr: &zeroOrOneExpr{ + pos: position{line: 126, col: 11, offset: 2366}, + expr: &actionExpr{ + pos: position{line: 127, col: 3, offset: 2370}, + run: (*parser).callonCreateTableStmt14, + expr: &seqExpr{ + pos: position{line: 127, col: 3, offset: 2370}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 127, col: 3, offset: 2370}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 127, col: 5, offset: 2372}, + name: "ColumnSchema", + }, + }, + &labeledExpr{ + pos: position{line: 128, col: 3, offset: 2387}, + label: "ss", + expr: &zeroOrMoreExpr{ + pos: position{line: 128, col: 6, offset: 2390}, + expr: &actionExpr{ + pos: position{line: 128, col: 8, offset: 2392}, + run: (*parser).callonCreateTableStmt20, + expr: &seqExpr{ + pos: position{line: 128, col: 8, offset: 2392}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 128, col: 8, offset: 2392}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 128, col: 10, offset: 2394}, + name: "SeparatorToken", + }, + &ruleRefExpr{ + pos: position{line: 128, col: 25, offset: 2409}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 128, col: 27, offset: 2411}, + label: "t", + expr: &ruleRefExpr{ + pos: position{line: 128, col: 29, offset: 2413}, + name: "ColumnSchema", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + &ruleRefExpr{ + pos: position{line: 131, col: 2, offset: 2485}, + name: "_", + }, + &litMatcher{ + pos: position{line: 131, col: 4, offset: 2487}, + val: ")", + ignoreCase: false, + }, + }, + }, + }, + }, + { + name: "ColumnSchema", + pos: position{line: 139, col: 1, offset: 2594}, + expr: &actionExpr{ + pos: position{line: 140, col: 4, offset: 2610}, + run: (*parser).callonColumnSchema1, + expr: &seqExpr{ + pos: position{line: 140, col: 4, offset: 2610}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 140, col: 4, offset: 2610}, + label: "i", + expr: &ruleRefExpr{ + pos: position{line: 140, col: 6, offset: 2612}, + name: "Identifier", + }, + }, + &ruleRefExpr{ + pos: position{line: 141, col: 2, offset: 2624}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 141, col: 4, offset: 2626}, + label: "t", + expr: &ruleRefExpr{ + pos: position{line: 141, col: 6, offset: 2628}, + name: "DataType", + }, + }, + &labeledExpr{ + pos: position{line: 142, col: 2, offset: 2638}, + label: "cs", + expr: &zeroOrMoreExpr{ + pos: position{line: 142, col: 5, offset: 2641}, + expr: &actionExpr{ + pos: position{line: 142, col: 7, offset: 2643}, + run: (*parser).callonColumnSchema10, + expr: &seqExpr{ + pos: position{line: 142, col: 7, offset: 2643}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 142, col: 7, offset: 2643}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 142, col: 9, offset: 2645}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 142, col: 11, offset: 2647}, + name: "ColumnConstraint", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "ColumnConstraint", + pos: position{line: 151, col: 1, offset: 2802}, + expr: &choiceExpr{ + pos: position{line: 152, col: 4, offset: 2822}, + alternatives: []interface{}{ + &ruleRefExpr{ + pos: position{line: 152, col: 4, offset: 2822}, + name: "PrimaryKeyClause", + }, + &ruleRefExpr{ + pos: position{line: 153, col: 4, offset: 2842}, + name: "NotNullClause", + }, + &ruleRefExpr{ + pos: position{line: 154, col: 4, offset: 2859}, + name: "UniqueClause", + }, + &ruleRefExpr{ + pos: position{line: 155, col: 4, offset: 2875}, + name: "DefaultClause", + }, + &ruleRefExpr{ + pos: position{line: 156, col: 4, offset: 2892}, + name: "ForeignClause", + }, + &ruleRefExpr{ + pos: position{line: 157, col: 4, offset: 2909}, + name: "AutoincrementClause", + }, + }, + }, + }, + { + name: "CreateIndexStmt", + pos: position{line: 159, col: 1, offset: 2930}, + expr: &actionExpr{ + pos: position{line: 160, col: 4, offset: 2949}, + run: (*parser).callonCreateIndexStmt1, + expr: &seqExpr{ + pos: position{line: 160, col: 4, offset: 2949}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 160, col: 4, offset: 2949}, + name: "CreateToken", + }, + &labeledExpr{ + pos: position{line: 161, col: 2, offset: 2962}, + label: "unique", + expr: &zeroOrOneExpr{ + pos: position{line: 161, col: 9, offset: 2969}, + expr: &actionExpr{ + pos: position{line: 161, col: 11, offset: 2971}, + run: (*parser).callonCreateIndexStmt6, + expr: &seqExpr{ + pos: position{line: 161, col: 11, offset: 2971}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 161, col: 11, offset: 2971}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 161, col: 13, offset: 2973}, + label: "u", + expr: &ruleRefExpr{ + pos: position{line: 161, col: 15, offset: 2975}, + name: "UniqueClause", + }, + }, + }, + }, + }, + }, + }, + &ruleRefExpr{ + pos: position{line: 162, col: 2, offset: 3010}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 162, col: 4, offset: 3012}, + name: "IndexToken", + }, + &ruleRefExpr{ + pos: position{line: 163, col: 2, offset: 3024}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 163, col: 4, offset: 3026}, + label: "index", + expr: &ruleRefExpr{ + pos: position{line: 163, col: 10, offset: 3032}, + name: "Identifier", + }, + }, + &ruleRefExpr{ + pos: position{line: 164, col: 2, offset: 3044}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 164, col: 4, offset: 3046}, + name: "OnToken", + }, + &ruleRefExpr{ + pos: position{line: 165, col: 2, offset: 3055}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 165, col: 4, offset: 3057}, + label: "table", + expr: &ruleRefExpr{ + pos: position{line: 165, col: 10, offset: 3063}, + name: "Identifier", + }, + }, + &ruleRefExpr{ + pos: position{line: 166, col: 2, offset: 3075}, + name: "_", + }, + &litMatcher{ + pos: position{line: 166, col: 4, offset: 3077}, + val: "(", + ignoreCase: false, + }, + &ruleRefExpr{ + pos: position{line: 166, col: 8, offset: 3081}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 166, col: 10, offset: 3083}, + label: "i", + expr: &ruleRefExpr{ + pos: position{line: 166, col: 12, offset: 3085}, + name: "Identifier", + }, + }, + &labeledExpr{ + pos: position{line: 166, col: 23, offset: 3096}, + label: "is", + expr: &zeroOrMoreExpr{ + pos: position{line: 166, col: 26, offset: 3099}, + expr: &actionExpr{ + pos: position{line: 166, col: 28, offset: 3101}, + run: (*parser).callonCreateIndexStmt28, + expr: &seqExpr{ + pos: position{line: 166, col: 28, offset: 3101}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 166, col: 28, offset: 3101}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 166, col: 30, offset: 3103}, + name: "SeparatorToken", + }, + &ruleRefExpr{ + pos: position{line: 166, col: 45, offset: 3118}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 166, col: 47, offset: 3120}, + label: "x", + expr: &ruleRefExpr{ + pos: position{line: 166, col: 49, offset: 3122}, + name: "Identifier", + }, + }, + }, + }, + }, + }, + }, + &ruleRefExpr{ + pos: position{line: 166, col: 81, offset: 3154}, + name: "_", + }, + &litMatcher{ + pos: position{line: 166, col: 83, offset: 3156}, + val: ")", + ignoreCase: false, + }, + }, + }, + }, + }, + { + name: "WhereClause", + pos: position{line: 184, col: 1, offset: 3446}, + expr: &actionExpr{ + pos: position{line: 185, col: 4, offset: 3461}, + run: (*parser).callonWhereClause1, + expr: &seqExpr{ + pos: position{line: 185, col: 4, offset: 3461}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 185, col: 4, offset: 3461}, + name: "WhereToken", + }, + &ruleRefExpr{ + pos: position{line: 185, col: 15, offset: 3472}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 185, col: 17, offset: 3474}, + label: "e", + expr: &ruleRefExpr{ + pos: position{line: 185, col: 19, offset: 3476}, + name: "Expr", + }, + }, + }, + }, + }, + }, + { + name: "OrderByClause", + pos: position{line: 188, col: 1, offset: 3528}, + expr: &actionExpr{ + pos: position{line: 189, col: 4, offset: 3545}, + run: (*parser).callonOrderByClause1, + expr: &seqExpr{ + pos: position{line: 189, col: 4, offset: 3545}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 189, col: 4, offset: 3545}, + name: "OrderToken", + }, + &ruleRefExpr{ + pos: position{line: 190, col: 2, offset: 3557}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 190, col: 4, offset: 3559}, + name: "ByToken", + }, + &ruleRefExpr{ + pos: position{line: 191, col: 2, offset: 3568}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 191, col: 4, offset: 3570}, + label: "f", + expr: &ruleRefExpr{ + pos: position{line: 191, col: 6, offset: 3572}, + name: "OrderColumn", + }, + }, + &labeledExpr{ + pos: position{line: 192, col: 2, offset: 3585}, + label: "fs", + expr: &zeroOrMoreExpr{ + pos: position{line: 192, col: 5, offset: 3588}, + expr: &actionExpr{ + pos: position{line: 192, col: 7, offset: 3590}, + run: (*parser).callonOrderByClause11, + expr: &seqExpr{ + pos: position{line: 192, col: 7, offset: 3590}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 192, col: 7, offset: 3590}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 192, col: 9, offset: 3592}, + name: "SeparatorToken", + }, + &ruleRefExpr{ + pos: position{line: 192, col: 24, offset: 3607}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 192, col: 26, offset: 3609}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 192, col: 28, offset: 3611}, + name: "OrderColumn", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "OrderColumn", + pos: position{line: 195, col: 1, offset: 3676}, + expr: &actionExpr{ + pos: position{line: 196, col: 4, offset: 3691}, + run: (*parser).callonOrderColumn1, + expr: &seqExpr{ + pos: position{line: 196, col: 4, offset: 3691}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 196, col: 4, offset: 3691}, + label: "i", + expr: &ruleRefExpr{ + pos: position{line: 196, col: 6, offset: 3693}, + name: "Expr", + }, + }, + &labeledExpr{ + pos: position{line: 197, col: 2, offset: 3699}, + label: "s", + expr: &zeroOrOneExpr{ + pos: position{line: 197, col: 4, offset: 3701}, + expr: &actionExpr{ + pos: position{line: 197, col: 6, offset: 3703}, + run: (*parser).callonOrderColumn7, + expr: &seqExpr{ + pos: position{line: 197, col: 6, offset: 3703}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 197, col: 6, offset: 3703}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 197, col: 8, offset: 3705}, + label: "t", + expr: &choiceExpr{ + pos: position{line: 197, col: 12, offset: 3709}, + alternatives: []interface{}{ + &ruleRefExpr{ + pos: position{line: 197, col: 12, offset: 3709}, + name: "AscToken", + }, + &ruleRefExpr{ + pos: position{line: 197, col: 23, offset: 3720}, + name: "DescToken", + }, + }, + }, + }, + }, + }, + }, + }, + }, + &labeledExpr{ + pos: position{line: 198, col: 2, offset: 3754}, + label: "n", + expr: &zeroOrOneExpr{ + pos: position{line: 198, col: 4, offset: 3756}, + expr: &actionExpr{ + pos: position{line: 198, col: 6, offset: 3758}, + run: (*parser).callonOrderColumn16, + expr: &seqExpr{ + pos: position{line: 198, col: 6, offset: 3758}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 198, col: 6, offset: 3758}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 198, col: 8, offset: 3760}, + name: "NullsToken", + }, + &ruleRefExpr{ + pos: position{line: 198, col: 19, offset: 3771}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 198, col: 21, offset: 3773}, + label: "l", + expr: &choiceExpr{ + pos: position{line: 198, col: 25, offset: 3777}, + alternatives: []interface{}{ + &ruleRefExpr{ + pos: position{line: 198, col: 25, offset: 3777}, + name: "LastToken", + }, + &ruleRefExpr{ + pos: position{line: 198, col: 37, offset: 3789}, + name: "FirstToken", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "GroupByClause", + pos: position{line: 207, col: 1, offset: 3988}, + expr: &actionExpr{ + pos: position{line: 208, col: 4, offset: 4005}, + run: (*parser).callonGroupByClause1, + expr: &seqExpr{ + pos: position{line: 208, col: 4, offset: 4005}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 208, col: 4, offset: 4005}, + name: "GroupToken", + }, + &ruleRefExpr{ + pos: position{line: 209, col: 2, offset: 4017}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 209, col: 4, offset: 4019}, + name: "ByToken", + }, + &ruleRefExpr{ + pos: position{line: 210, col: 2, offset: 4028}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 210, col: 4, offset: 4030}, + label: "i", + expr: &ruleRefExpr{ + pos: position{line: 210, col: 6, offset: 4032}, + name: "Expr", + }, + }, + &labeledExpr{ + pos: position{line: 211, col: 2, offset: 4038}, + label: "is", + expr: &zeroOrMoreExpr{ + pos: position{line: 211, col: 5, offset: 4041}, + expr: &actionExpr{ + pos: position{line: 211, col: 7, offset: 4043}, + run: (*parser).callonGroupByClause11, + expr: &seqExpr{ + pos: position{line: 211, col: 7, offset: 4043}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 211, col: 7, offset: 4043}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 211, col: 9, offset: 4045}, + name: "SeparatorToken", + }, + &ruleRefExpr{ + pos: position{line: 211, col: 24, offset: 4060}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 211, col: 26, offset: 4062}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 211, col: 28, offset: 4064}, + name: "Expr", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "OffsetClause", + pos: position{line: 214, col: 1, offset: 4168}, + expr: &actionExpr{ + pos: position{line: 215, col: 4, offset: 4184}, + run: (*parser).callonOffsetClause1, + expr: &seqExpr{ + pos: position{line: 215, col: 4, offset: 4184}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 215, col: 4, offset: 4184}, + name: "OffsetToken", + }, + &ruleRefExpr{ + pos: position{line: 215, col: 16, offset: 4196}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 215, col: 18, offset: 4198}, + label: "i", + expr: &ruleRefExpr{ + pos: position{line: 215, col: 20, offset: 4200}, + name: "Integer", + }, + }, + }, + }, + }, + }, + { + name: "LimitClause", + pos: position{line: 218, col: 1, offset: 4271}, + expr: &actionExpr{ + pos: position{line: 219, col: 4, offset: 4286}, + run: (*parser).callonLimitClause1, + expr: &seqExpr{ + pos: position{line: 219, col: 4, offset: 4286}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 219, col: 4, offset: 4286}, + name: "LimitToken", + }, + &ruleRefExpr{ + pos: position{line: 219, col: 15, offset: 4297}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 219, col: 17, offset: 4299}, + label: "i", + expr: &ruleRefExpr{ + pos: position{line: 219, col: 19, offset: 4301}, + name: "Integer", + }, + }, + }, + }, + }, + }, + { + name: "InsertWithColumnClause", + pos: position{line: 222, col: 1, offset: 4371}, + expr: &actionExpr{ + pos: position{line: 223, col: 4, offset: 4397}, + run: (*parser).callonInsertWithColumnClause1, + expr: &seqExpr{ + pos: position{line: 223, col: 4, offset: 4397}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 223, col: 4, offset: 4397}, + label: "cs", + expr: &zeroOrOneExpr{ + pos: position{line: 223, col: 7, offset: 4400}, + expr: &actionExpr{ + pos: position{line: 223, col: 9, offset: 4402}, + run: (*parser).callonInsertWithColumnClause5, + expr: &seqExpr{ + pos: position{line: 223, col: 9, offset: 4402}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 223, col: 9, offset: 4402}, + val: "(", + ignoreCase: false, + }, + &ruleRefExpr{ + pos: position{line: 224, col: 4, offset: 4409}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 224, col: 6, offset: 4411}, + label: "f", + expr: &ruleRefExpr{ + pos: position{line: 224, col: 8, offset: 4413}, + name: "Identifier", + }, + }, + &labeledExpr{ + pos: position{line: 225, col: 4, offset: 4427}, + label: "fs", + expr: &zeroOrMoreExpr{ + pos: position{line: 225, col: 7, offset: 4430}, + expr: &actionExpr{ + pos: position{line: 225, col: 9, offset: 4432}, + run: (*parser).callonInsertWithColumnClause13, + expr: &seqExpr{ + pos: position{line: 225, col: 9, offset: 4432}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 225, col: 9, offset: 4432}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 225, col: 11, offset: 4434}, + name: "SeparatorToken", + }, + &ruleRefExpr{ + pos: position{line: 225, col: 26, offset: 4449}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 225, col: 28, offset: 4451}, + label: "x", + expr: &ruleRefExpr{ + pos: position{line: 225, col: 30, offset: 4453}, + name: "Identifier", + }, + }, + }, + }, + }, + }, + }, + &ruleRefExpr{ + pos: position{line: 226, col: 4, offset: 4488}, + name: "_", + }, + &litMatcher{ + pos: position{line: 226, col: 6, offset: 4490}, + val: ")", + ignoreCase: false, + }, + &ruleRefExpr{ + pos: position{line: 227, col: 4, offset: 4497}, + name: "_", + }, + }, + }, + }, + }, + }, + &ruleRefExpr{ + pos: position{line: 229, col: 3, offset: 4537}, + name: "ValuesToken", + }, + &ruleRefExpr{ + pos: position{line: 230, col: 2, offset: 4550}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 230, col: 4, offset: 4552}, + label: "v", + expr: &ruleRefExpr{ + pos: position{line: 230, col: 6, offset: 4554}, + name: "InsertValue", + }, + }, + &labeledExpr{ + pos: position{line: 231, col: 2, offset: 4567}, + label: "vs", + expr: &zeroOrMoreExpr{ + pos: position{line: 231, col: 5, offset: 4570}, + expr: &actionExpr{ + pos: position{line: 231, col: 7, offset: 4572}, + run: (*parser).callonInsertWithColumnClause29, + expr: &seqExpr{ + pos: position{line: 231, col: 7, offset: 4572}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 231, col: 7, offset: 4572}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 231, col: 9, offset: 4574}, + name: "SeparatorToken", + }, + &ruleRefExpr{ + pos: position{line: 231, col: 24, offset: 4589}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 231, col: 26, offset: 4591}, + label: "y", + expr: &ruleRefExpr{ + pos: position{line: 231, col: 28, offset: 4593}, + name: "InsertValue", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "InsertWithDefaultClause", + pos: position{line: 239, col: 1, offset: 4724}, + expr: &actionExpr{ + pos: position{line: 240, col: 4, offset: 4751}, + run: (*parser).callonInsertWithDefaultClause1, + expr: &seqExpr{ + pos: position{line: 240, col: 4, offset: 4751}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 240, col: 4, offset: 4751}, + name: "DefaultToken", + }, + &ruleRefExpr{ + pos: position{line: 240, col: 17, offset: 4764}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 240, col: 19, offset: 4766}, + name: "ValuesToken", + }, + }, + }, + }, + }, + { + name: "PrimaryKeyClause", + pos: position{line: 243, col: 1, offset: 4825}, + expr: &actionExpr{ + pos: position{line: 244, col: 4, offset: 4845}, + run: (*parser).callonPrimaryKeyClause1, + expr: &seqExpr{ + pos: position{line: 244, col: 4, offset: 4845}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 244, col: 4, offset: 4845}, + name: "PrimaryToken", + }, + &ruleRefExpr{ + pos: position{line: 244, col: 17, offset: 4858}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 244, col: 19, offset: 4860}, + name: "KeyToken", + }, + }, + }, + }, + }, + { + name: "NotNullClause", + pos: position{line: 247, col: 1, offset: 4906}, + expr: &actionExpr{ + pos: position{line: 248, col: 4, offset: 4923}, + run: (*parser).callonNotNullClause1, + expr: &seqExpr{ + pos: position{line: 248, col: 4, offset: 4923}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 248, col: 4, offset: 4923}, + name: "NotToken", + }, + &ruleRefExpr{ + pos: position{line: 248, col: 13, offset: 4932}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 248, col: 15, offset: 4934}, + name: "NullToken", + }, + }, + }, + }, + }, + { + name: "UniqueClause", + pos: position{line: 251, col: 1, offset: 4981}, + expr: &actionExpr{ + pos: position{line: 252, col: 4, offset: 4997}, + run: (*parser).callonUniqueClause1, + expr: &ruleRefExpr{ + pos: position{line: 252, col: 4, offset: 4997}, + name: "UniqueToken", + }, + }, + }, + { + name: "DefaultClause", + pos: position{line: 255, col: 1, offset: 5045}, + expr: &actionExpr{ + pos: position{line: 256, col: 4, offset: 5062}, + run: (*parser).callonDefaultClause1, + expr: &seqExpr{ + pos: position{line: 256, col: 4, offset: 5062}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 256, col: 4, offset: 5062}, + name: "DefaultToken", + }, + &ruleRefExpr{ + pos: position{line: 256, col: 17, offset: 5075}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 256, col: 19, offset: 5077}, + label: "e", + expr: &ruleRefExpr{ + pos: position{line: 256, col: 21, offset: 5079}, + name: "Expr", + }, + }, + }, + }, + }, + }, + { + name: "ForeignClause", + pos: position{line: 259, col: 1, offset: 5129}, + expr: &actionExpr{ + pos: position{line: 260, col: 4, offset: 5146}, + run: (*parser).callonForeignClause1, + expr: &seqExpr{ + pos: position{line: 260, col: 4, offset: 5146}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 260, col: 4, offset: 5146}, + name: "ReferencesToken", + }, + &ruleRefExpr{ + pos: position{line: 260, col: 20, offset: 5162}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 260, col: 22, offset: 5164}, + label: "t", + expr: &ruleRefExpr{ + pos: position{line: 260, col: 24, offset: 5166}, + name: "Identifier", + }, + }, + &ruleRefExpr{ + pos: position{line: 260, col: 35, offset: 5177}, + name: "_", + }, + &litMatcher{ + pos: position{line: 260, col: 37, offset: 5179}, + val: "(", + ignoreCase: false, + }, + &ruleRefExpr{ + pos: position{line: 260, col: 41, offset: 5183}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 260, col: 43, offset: 5185}, + label: "f", + expr: &ruleRefExpr{ + pos: position{line: 260, col: 45, offset: 5187}, + name: "Identifier", + }, + }, + &ruleRefExpr{ + pos: position{line: 260, col: 56, offset: 5198}, + name: "_", + }, + &litMatcher{ + pos: position{line: 260, col: 58, offset: 5200}, + val: ")", + ignoreCase: false, + }, + }, + }, + }, + }, + { + name: "AutoincrementClause", + pos: position{line: 268, col: 1, offset: 5304}, + expr: &actionExpr{ + pos: position{line: 269, col: 4, offset: 5327}, + run: (*parser).callonAutoincrementClause1, + expr: &ruleRefExpr{ + pos: position{line: 269, col: 4, offset: 5327}, + name: "AutoincrementToken", + }, + }, + }, + { + name: "Expr", + pos: position{line: 273, col: 1, offset: 5406}, + expr: &ruleRefExpr{ + pos: position{line: 274, col: 4, offset: 5414}, + name: "LogicExpr", + }, + }, + { + name: "ExprWithDefault", + pos: position{line: 276, col: 1, offset: 5425}, + expr: &choiceExpr{ + pos: position{line: 277, col: 4, offset: 5444}, + alternatives: []interface{}{ + &actionExpr{ + pos: position{line: 277, col: 4, offset: 5444}, + run: (*parser).callonExprWithDefault2, + expr: &seqExpr{ + pos: position{line: 277, col: 4, offset: 5444}, + exprs: []interface{}{ + &andExpr{ + pos: position{line: 277, col: 4, offset: 5444}, + expr: &ruleRefExpr{ + pos: position{line: 277, col: 6, offset: 5446}, + name: "DefaultLiteral", + }, + }, + &labeledExpr{ + pos: position{line: 277, col: 22, offset: 5462}, + label: "d", + expr: &ruleRefExpr{ + pos: position{line: 277, col: 24, offset: 5464}, + name: "DefaultLiteral", + }, + }, + }, + }, + }, + &ruleRefExpr{ + pos: position{line: 278, col: 4, offset: 5500}, + name: "Expr", + }, + }, + }, + }, + { + name: "LogicExpr", + pos: position{line: 280, col: 1, offset: 5506}, + expr: &ruleRefExpr{ + pos: position{line: 281, col: 4, offset: 5519}, + name: "LogicExpr4", + }, + }, + { + name: "LogicExpr4", + pos: position{line: 283, col: 1, offset: 5531}, + expr: &actionExpr{ + pos: position{line: 284, col: 4, offset: 5545}, + run: (*parser).callonLogicExpr41, + expr: &seqExpr{ + pos: position{line: 284, col: 4, offset: 5545}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 284, col: 4, offset: 5545}, + label: "o", + expr: &ruleRefExpr{ + pos: position{line: 284, col: 6, offset: 5547}, + name: "LogicExpr3", + }, + }, + &labeledExpr{ + pos: position{line: 285, col: 3, offset: 5560}, + label: "os", + expr: &zeroOrMoreExpr{ + pos: position{line: 285, col: 6, offset: 5563}, + expr: &actionExpr{ + pos: position{line: 285, col: 8, offset: 5565}, + run: (*parser).callonLogicExpr47, + expr: &seqExpr{ + pos: position{line: 285, col: 8, offset: 5565}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 285, col: 8, offset: 5565}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 285, col: 10, offset: 5567}, + label: "op", + expr: &ruleRefExpr{ + pos: position{line: 285, col: 13, offset: 5570}, + name: "OrOperator", + }, + }, + &ruleRefExpr{ + pos: position{line: 285, col: 24, offset: 5581}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 285, col: 26, offset: 5583}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 285, col: 28, offset: 5585}, + name: "LogicExpr3", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "LogicExpr3", + pos: position{line: 288, col: 1, offset: 5678}, + expr: &actionExpr{ + pos: position{line: 289, col: 4, offset: 5692}, + run: (*parser).callonLogicExpr31, + expr: &seqExpr{ + pos: position{line: 289, col: 4, offset: 5692}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 289, col: 4, offset: 5692}, + label: "o", + expr: &ruleRefExpr{ + pos: position{line: 289, col: 6, offset: 5694}, + name: "LogicExpr2", + }, + }, + &labeledExpr{ + pos: position{line: 290, col: 3, offset: 5707}, + label: "os", + expr: &zeroOrMoreExpr{ + pos: position{line: 290, col: 6, offset: 5710}, + expr: &actionExpr{ + pos: position{line: 290, col: 8, offset: 5712}, + run: (*parser).callonLogicExpr37, + expr: &seqExpr{ + pos: position{line: 290, col: 8, offset: 5712}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 290, col: 8, offset: 5712}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 290, col: 10, offset: 5714}, + label: "op", + expr: &ruleRefExpr{ + pos: position{line: 290, col: 13, offset: 5717}, + name: "AndOperator", + }, + }, + &ruleRefExpr{ + pos: position{line: 290, col: 25, offset: 5729}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 290, col: 27, offset: 5731}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 290, col: 29, offset: 5733}, + name: "LogicExpr2", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "LogicExpr2", + pos: position{line: 293, col: 1, offset: 5826}, + expr: &choiceExpr{ + pos: position{line: 294, col: 4, offset: 5840}, + alternatives: []interface{}{ + &actionExpr{ + pos: position{line: 294, col: 4, offset: 5840}, + run: (*parser).callonLogicExpr22, + expr: &seqExpr{ + pos: position{line: 294, col: 4, offset: 5840}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 294, col: 4, offset: 5840}, + label: "op", + expr: &ruleRefExpr{ + pos: position{line: 294, col: 7, offset: 5843}, + name: "NotOperator", + }, + }, + &ruleRefExpr{ + pos: position{line: 294, col: 19, offset: 5855}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 294, col: 21, offset: 5857}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 294, col: 23, offset: 5859}, + name: "LogicExpr2", + }, + }, + }, + }, + }, + &ruleRefExpr{ + pos: position{line: 296, col: 4, offset: 5909}, + name: "LogicExpr1", + }, + }, + }, + }, + { + name: "LogicExpr1", + pos: position{line: 298, col: 1, offset: 5921}, + expr: &actionExpr{ + pos: position{line: 299, col: 4, offset: 5935}, + run: (*parser).callonLogicExpr11, + expr: &seqExpr{ + pos: position{line: 299, col: 4, offset: 5935}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 299, col: 4, offset: 5935}, + label: "o", + expr: &ruleRefExpr{ + pos: position{line: 299, col: 6, offset: 5937}, + name: "ArithmeticExpr", + }, + }, + &labeledExpr{ + pos: position{line: 299, col: 21, offset: 5952}, + label: "os", + expr: &zeroOrMoreExpr{ + pos: position{line: 299, col: 24, offset: 5955}, + expr: &actionExpr{ + pos: position{line: 299, col: 26, offset: 5957}, + run: (*parser).callonLogicExpr17, + expr: &seqExpr{ + pos: position{line: 299, col: 26, offset: 5957}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 299, col: 26, offset: 5957}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 299, col: 28, offset: 5959}, + label: "l", + expr: &ruleRefExpr{ + pos: position{line: 299, col: 30, offset: 5961}, + name: "LogicExpr1Op", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "LogicExpr1Op", + pos: position{line: 302, col: 1, offset: 6038}, + expr: &choiceExpr{ + pos: position{line: 303, col: 4, offset: 6054}, + alternatives: []interface{}{ + &ruleRefExpr{ + pos: position{line: 303, col: 4, offset: 6054}, + name: "LogicExpr1In", + }, + &ruleRefExpr{ + pos: position{line: 304, col: 4, offset: 6070}, + name: "LogicExpr1Null", + }, + &ruleRefExpr{ + pos: position{line: 305, col: 4, offset: 6088}, + name: "LogicExpr1Like", + }, + &ruleRefExpr{ + pos: position{line: 306, col: 4, offset: 6106}, + name: "LogicExpr1Cmp", + }, + }, + }, + }, + { + name: "LogicExpr1In", + pos: position{line: 308, col: 1, offset: 6121}, + expr: &actionExpr{ + pos: position{line: 309, col: 4, offset: 6137}, + run: (*parser).callonLogicExpr1In1, + expr: &seqExpr{ + pos: position{line: 309, col: 4, offset: 6137}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 309, col: 4, offset: 6137}, + label: "n", + expr: &zeroOrOneExpr{ + pos: position{line: 309, col: 6, offset: 6139}, + expr: &actionExpr{ + pos: position{line: 309, col: 8, offset: 6141}, + run: (*parser).callonLogicExpr1In5, + expr: &seqExpr{ + pos: position{line: 309, col: 8, offset: 6141}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 309, col: 8, offset: 6141}, + label: "t", + expr: &ruleRefExpr{ + pos: position{line: 309, col: 10, offset: 6143}, + name: "NotOperator", + }, + }, + &ruleRefExpr{ + pos: position{line: 309, col: 22, offset: 6155}, + name: "_", + }, + }, + }, + }, + }, + }, + &ruleRefExpr{ + pos: position{line: 309, col: 45, offset: 6178}, + name: "InToken", + }, + &ruleRefExpr{ + pos: position{line: 309, col: 53, offset: 6186}, + name: "_", + }, + &litMatcher{ + pos: position{line: 309, col: 55, offset: 6188}, + val: "(", + ignoreCase: false, + }, + &ruleRefExpr{ + pos: position{line: 309, col: 59, offset: 6192}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 309, col: 61, offset: 6194}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 309, col: 63, offset: 6196}, + name: "MultiExpr", + }, + }, + &ruleRefExpr{ + pos: position{line: 309, col: 73, offset: 6206}, + name: "_", + }, + &litMatcher{ + pos: position{line: 309, col: 75, offset: 6208}, + val: ")", + ignoreCase: false, + }, + }, + }, + }, + }, + { + name: "LogicExpr1Null", + pos: position{line: 318, col: 1, offset: 6326}, + expr: &actionExpr{ + pos: position{line: 319, col: 4, offset: 6344}, + run: (*parser).callonLogicExpr1Null1, + expr: &seqExpr{ + pos: position{line: 319, col: 4, offset: 6344}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 319, col: 4, offset: 6344}, + name: "IsToken", + }, + &labeledExpr{ + pos: position{line: 319, col: 12, offset: 6352}, + label: "n", + expr: &zeroOrOneExpr{ + pos: position{line: 319, col: 14, offset: 6354}, + expr: &actionExpr{ + pos: position{line: 319, col: 16, offset: 6356}, + run: (*parser).callonLogicExpr1Null6, + expr: &seqExpr{ + pos: position{line: 319, col: 16, offset: 6356}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 319, col: 16, offset: 6356}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 319, col: 18, offset: 6358}, + label: "t", + expr: &ruleRefExpr{ + pos: position{line: 319, col: 20, offset: 6360}, + name: "NotOperator", + }, + }, + }, + }, + }, + }, + }, + &ruleRefExpr{ + pos: position{line: 319, col: 53, offset: 6393}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 319, col: 55, offset: 6395}, + name: "NullToken", + }, + }, + }, + }, + }, + { + name: "LogicExpr1Like", + pos: position{line: 328, col: 1, offset: 6533}, + expr: &actionExpr{ + pos: position{line: 329, col: 4, offset: 6551}, + run: (*parser).callonLogicExpr1Like1, + expr: &seqExpr{ + pos: position{line: 329, col: 4, offset: 6551}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 329, col: 4, offset: 6551}, + label: "n", + expr: &zeroOrOneExpr{ + pos: position{line: 329, col: 6, offset: 6553}, + expr: &actionExpr{ + pos: position{line: 329, col: 8, offset: 6555}, + run: (*parser).callonLogicExpr1Like5, + expr: &seqExpr{ + pos: position{line: 329, col: 8, offset: 6555}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 329, col: 8, offset: 6555}, + label: "t", + expr: &ruleRefExpr{ + pos: position{line: 329, col: 10, offset: 6557}, + name: "NotOperator", + }, + }, + &ruleRefExpr{ + pos: position{line: 329, col: 22, offset: 6569}, + name: "_", + }, + }, + }, + }, + }, + }, + &ruleRefExpr{ + pos: position{line: 329, col: 45, offset: 6592}, + name: "LikeToken", + }, + &ruleRefExpr{ + pos: position{line: 329, col: 55, offset: 6602}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 329, col: 57, offset: 6604}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 329, col: 59, offset: 6606}, + name: "Expr", + }, + }, + }, + }, + }, + }, + { + name: "LogicExpr1Cmp", + pos: position{line: 338, col: 1, offset: 6727}, + expr: &actionExpr{ + pos: position{line: 339, col: 4, offset: 6744}, + run: (*parser).callonLogicExpr1Cmp1, + expr: &seqExpr{ + pos: position{line: 339, col: 4, offset: 6744}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 339, col: 4, offset: 6744}, + label: "op", + expr: &ruleRefExpr{ + pos: position{line: 339, col: 7, offset: 6747}, + name: "CmpOperator", + }, + }, + &ruleRefExpr{ + pos: position{line: 339, col: 19, offset: 6759}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 339, col: 21, offset: 6761}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 339, col: 23, offset: 6763}, + name: "ArithmeticExpr", + }, + }, + }, + }, + }, + }, + { + name: "ArithmeticExpr", + pos: position{line: 342, col: 1, offset: 6815}, + expr: &ruleRefExpr{ + pos: position{line: 343, col: 4, offset: 6833}, + name: "ArithmeticExpr3", + }, + }, + { + name: "ArithmeticExpr3", + pos: position{line: 345, col: 1, offset: 6850}, + expr: &actionExpr{ + pos: position{line: 346, col: 4, offset: 6869}, + run: (*parser).callonArithmeticExpr31, + expr: &seqExpr{ + pos: position{line: 346, col: 4, offset: 6869}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 346, col: 4, offset: 6869}, + label: "o", + expr: &ruleRefExpr{ + pos: position{line: 346, col: 6, offset: 6871}, + name: "ArithmeticExpr2", + }, + }, + &labeledExpr{ + pos: position{line: 346, col: 22, offset: 6887}, + label: "os", + expr: &zeroOrMoreExpr{ + pos: position{line: 346, col: 25, offset: 6890}, + expr: &actionExpr{ + pos: position{line: 346, col: 27, offset: 6892}, + run: (*parser).callonArithmeticExpr37, + expr: &seqExpr{ + pos: position{line: 346, col: 27, offset: 6892}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 346, col: 27, offset: 6892}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 346, col: 29, offset: 6894}, + label: "op", + expr: &ruleRefExpr{ + pos: position{line: 346, col: 32, offset: 6897}, + name: "ConcatOperator", + }, + }, + &ruleRefExpr{ + pos: position{line: 346, col: 47, offset: 6912}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 346, col: 49, offset: 6914}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 346, col: 51, offset: 6916}, + name: "ArithmeticExpr2", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "ArithmeticExpr2", + pos: position{line: 349, col: 1, offset: 7014}, + expr: &actionExpr{ + pos: position{line: 350, col: 4, offset: 7033}, + run: (*parser).callonArithmeticExpr21, + expr: &seqExpr{ + pos: position{line: 350, col: 4, offset: 7033}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 350, col: 4, offset: 7033}, + label: "o", + expr: &ruleRefExpr{ + pos: position{line: 350, col: 6, offset: 7035}, + name: "ArithmeticExpr1", + }, + }, + &labeledExpr{ + pos: position{line: 350, col: 22, offset: 7051}, + label: "os", + expr: &zeroOrMoreExpr{ + pos: position{line: 350, col: 25, offset: 7054}, + expr: &actionExpr{ + pos: position{line: 350, col: 27, offset: 7056}, + run: (*parser).callonArithmeticExpr27, + expr: &seqExpr{ + pos: position{line: 350, col: 27, offset: 7056}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 350, col: 27, offset: 7056}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 350, col: 29, offset: 7058}, + label: "op", + expr: &ruleRefExpr{ + pos: position{line: 350, col: 32, offset: 7061}, + name: "AddSubOperator", + }, + }, + &ruleRefExpr{ + pos: position{line: 350, col: 47, offset: 7076}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 350, col: 49, offset: 7078}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 350, col: 51, offset: 7080}, + name: "ArithmeticExpr1", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "ArithmeticExpr1", + pos: position{line: 353, col: 1, offset: 7178}, + expr: &actionExpr{ + pos: position{line: 354, col: 4, offset: 7197}, + run: (*parser).callonArithmeticExpr11, + expr: &seqExpr{ + pos: position{line: 354, col: 4, offset: 7197}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 354, col: 4, offset: 7197}, + label: "o", + expr: &ruleRefExpr{ + pos: position{line: 354, col: 6, offset: 7199}, + name: "Operand", + }, + }, + &labeledExpr{ + pos: position{line: 354, col: 14, offset: 7207}, + label: "os", + expr: &zeroOrMoreExpr{ + pos: position{line: 354, col: 17, offset: 7210}, + expr: &actionExpr{ + pos: position{line: 354, col: 19, offset: 7212}, + run: (*parser).callonArithmeticExpr17, + expr: &seqExpr{ + pos: position{line: 354, col: 19, offset: 7212}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 354, col: 19, offset: 7212}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 354, col: 21, offset: 7214}, + label: "op", + expr: &ruleRefExpr{ + pos: position{line: 354, col: 24, offset: 7217}, + name: "MulDivModOperator", + }, + }, + &ruleRefExpr{ + pos: position{line: 354, col: 42, offset: 7235}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 354, col: 44, offset: 7237}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 354, col: 46, offset: 7239}, + name: "Operand", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "MultiExpr", + pos: position{line: 357, col: 1, offset: 7329}, + expr: &actionExpr{ + pos: position{line: 358, col: 4, offset: 7342}, + run: (*parser).callonMultiExpr1, + expr: &seqExpr{ + pos: position{line: 358, col: 4, offset: 7342}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 358, col: 4, offset: 7342}, + label: "x", + expr: &ruleRefExpr{ + pos: position{line: 358, col: 6, offset: 7344}, + name: "Expr", + }, + }, + &labeledExpr{ + pos: position{line: 358, col: 11, offset: 7349}, + label: "xs", + expr: &zeroOrMoreExpr{ + pos: position{line: 358, col: 14, offset: 7352}, + expr: &actionExpr{ + pos: position{line: 358, col: 16, offset: 7354}, + run: (*parser).callonMultiExpr7, + expr: &seqExpr{ + pos: position{line: 358, col: 16, offset: 7354}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 358, col: 16, offset: 7354}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 358, col: 18, offset: 7356}, + name: "SeparatorToken", + }, + &ruleRefExpr{ + pos: position{line: 358, col: 33, offset: 7371}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 358, col: 35, offset: 7373}, + label: "e", + expr: &ruleRefExpr{ + pos: position{line: 358, col: 37, offset: 7375}, + name: "Expr", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "MultiExprWithDefault", + pos: position{line: 361, col: 1, offset: 7433}, + expr: &actionExpr{ + pos: position{line: 362, col: 4, offset: 7457}, + run: (*parser).callonMultiExprWithDefault1, + expr: &seqExpr{ + pos: position{line: 362, col: 4, offset: 7457}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 362, col: 4, offset: 7457}, + label: "x", + expr: &ruleRefExpr{ + pos: position{line: 362, col: 6, offset: 7459}, + name: "ExprWithDefault", + }, + }, + &labeledExpr{ + pos: position{line: 362, col: 22, offset: 7475}, + label: "xs", + expr: &zeroOrMoreExpr{ + pos: position{line: 362, col: 25, offset: 7478}, + expr: &actionExpr{ + pos: position{line: 362, col: 27, offset: 7480}, + run: (*parser).callonMultiExprWithDefault7, + expr: &seqExpr{ + pos: position{line: 362, col: 27, offset: 7480}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 362, col: 27, offset: 7480}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 362, col: 29, offset: 7482}, + name: "SeparatorToken", + }, + &ruleRefExpr{ + pos: position{line: 362, col: 44, offset: 7497}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 362, col: 46, offset: 7499}, + label: "e", + expr: &ruleRefExpr{ + pos: position{line: 362, col: 48, offset: 7501}, + name: "ExprWithDefault", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "Operand", + pos: position{line: 365, col: 1, offset: 7570}, + expr: &choiceExpr{ + pos: position{line: 366, col: 4, offset: 7581}, + alternatives: []interface{}{ + &actionExpr{ + pos: position{line: 366, col: 4, offset: 7581}, + run: (*parser).callonOperand2, + expr: &seqExpr{ + pos: position{line: 366, col: 4, offset: 7581}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 366, col: 4, offset: 7581}, + label: "op", + expr: &ruleRefExpr{ + pos: position{line: 366, col: 7, offset: 7584}, + name: "UnaryOperator", + }, + }, + &ruleRefExpr{ + pos: position{line: 366, col: 21, offset: 7598}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 366, col: 23, offset: 7600}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 366, col: 25, offset: 7602}, + name: "Operand", + }, + }, + }, + }, + }, + &actionExpr{ + pos: position{line: 367, col: 4, offset: 7648}, + run: (*parser).callonOperand9, + expr: &seqExpr{ + pos: position{line: 367, col: 4, offset: 7648}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 367, col: 4, offset: 7648}, + val: "(", + ignoreCase: false, + }, + &ruleRefExpr{ + pos: position{line: 367, col: 8, offset: 7652}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 367, col: 10, offset: 7654}, + label: "e", + expr: &ruleRefExpr{ + pos: position{line: 367, col: 12, offset: 7656}, + name: "Expr", + }, + }, + &ruleRefExpr{ + pos: position{line: 367, col: 17, offset: 7661}, + name: "_", + }, + &litMatcher{ + pos: position{line: 367, col: 19, offset: 7663}, + val: ")", + ignoreCase: false, + }, + }, + }, + }, + &actionExpr{ + pos: position{line: 368, col: 4, offset: 7688}, + run: (*parser).callonOperand17, + expr: &seqExpr{ + pos: position{line: 368, col: 4, offset: 7688}, + exprs: []interface{}{ + &andExpr{ + pos: position{line: 368, col: 4, offset: 7688}, + expr: &ruleRefExpr{ + pos: position{line: 368, col: 6, offset: 7690}, + name: "CastToken", + }, + }, + &labeledExpr{ + pos: position{line: 368, col: 17, offset: 7701}, + label: "t", + expr: &ruleRefExpr{ + pos: position{line: 368, col: 19, offset: 7703}, + name: "TypeCast", + }, + }, + }, + }, + }, + &ruleRefExpr{ + pos: position{line: 369, col: 4, offset: 7733}, + name: "FunctionCall", + }, + &ruleRefExpr{ + pos: position{line: 370, col: 4, offset: 7749}, + name: "Value", + }, + &ruleRefExpr{ + pos: position{line: 371, col: 4, offset: 7758}, + name: "Identifier", + }, + }, + }, + }, + { + name: "TypeCast", + pos: position{line: 373, col: 1, offset: 7770}, + expr: &actionExpr{ + pos: position{line: 374, col: 4, offset: 7782}, + run: (*parser).callonTypeCast1, + expr: &seqExpr{ + pos: position{line: 374, col: 4, offset: 7782}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 374, col: 4, offset: 7782}, + name: "CastToken", + }, + &ruleRefExpr{ + pos: position{line: 374, col: 14, offset: 7792}, + name: "_", + }, + &litMatcher{ + pos: position{line: 374, col: 16, offset: 7794}, + val: "(", + ignoreCase: false, + }, + &ruleRefExpr{ + pos: position{line: 374, col: 20, offset: 7798}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 374, col: 22, offset: 7800}, + label: "o", + expr: &ruleRefExpr{ + pos: position{line: 374, col: 24, offset: 7802}, + name: "Expr", + }, + }, + &ruleRefExpr{ + pos: position{line: 374, col: 29, offset: 7807}, + name: "_", + }, + &ruleRefExpr{ + pos: position{line: 374, col: 31, offset: 7809}, + name: "AsToken", + }, + &ruleRefExpr{ + pos: position{line: 374, col: 39, offset: 7817}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 374, col: 41, offset: 7819}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 374, col: 43, offset: 7821}, + name: "DataType", + }, + }, + &ruleRefExpr{ + pos: position{line: 374, col: 52, offset: 7830}, + name: "_", + }, + &litMatcher{ + pos: position{line: 374, col: 54, offset: 7832}, + val: ")", + ignoreCase: false, + }, + }, + }, + }, + }, + { + name: "FunctionCall", + pos: position{line: 377, col: 1, offset: 7906}, + expr: &actionExpr{ + pos: position{line: 378, col: 4, offset: 7922}, + run: (*parser).callonFunctionCall1, + expr: &seqExpr{ + pos: position{line: 378, col: 4, offset: 7922}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 378, col: 4, offset: 7922}, + label: "i", + expr: &ruleRefExpr{ + pos: position{line: 378, col: 6, offset: 7924}, + name: "Identifier", + }, + }, + &ruleRefExpr{ + pos: position{line: 378, col: 17, offset: 7935}, + name: "_", + }, + &litMatcher{ + pos: position{line: 378, col: 19, offset: 7937}, + val: "(", + ignoreCase: false, + }, + &ruleRefExpr{ + pos: position{line: 378, col: 23, offset: 7941}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 378, col: 25, offset: 7943}, + label: "r", + expr: &zeroOrOneExpr{ + pos: position{line: 378, col: 27, offset: 7945}, + expr: &ruleRefExpr{ + pos: position{line: 378, col: 27, offset: 7945}, + name: "FunctionArgs", + }, + }, + }, + &ruleRefExpr{ + pos: position{line: 378, col: 41, offset: 7959}, + name: "_", + }, + &litMatcher{ + pos: position{line: 378, col: 43, offset: 7961}, + val: ")", + ignoreCase: false, + }, + }, + }, + }, + }, + { + name: "FunctionArgs", + pos: position{line: 381, col: 1, offset: 8039}, + expr: &choiceExpr{ + pos: position{line: 382, col: 4, offset: 8055}, + alternatives: []interface{}{ + &actionExpr{ + pos: position{line: 382, col: 4, offset: 8055}, + run: (*parser).callonFunctionArgs2, + expr: &labeledExpr{ + pos: position{line: 382, col: 4, offset: 8055}, + label: "a", + expr: &ruleRefExpr{ + pos: position{line: 382, col: 6, offset: 8057}, + name: "AnyLiteral", + }, + }, + }, + &ruleRefExpr{ + pos: position{line: 383, col: 4, offset: 8104}, + name: "MultiExpr", + }, + }, + }, + }, + { + name: "Assignment", + pos: position{line: 385, col: 1, offset: 8115}, + expr: &actionExpr{ + pos: position{line: 386, col: 4, offset: 8129}, + run: (*parser).callonAssignment1, + expr: &seqExpr{ + pos: position{line: 386, col: 4, offset: 8129}, + exprs: []interface{}{ + &labeledExpr{ + pos: position{line: 386, col: 4, offset: 8129}, + label: "i", + expr: &ruleRefExpr{ + pos: position{line: 386, col: 6, offset: 8131}, + name: "Identifier", + }, + }, + &ruleRefExpr{ + pos: position{line: 386, col: 17, offset: 8142}, + name: "_", + }, + &litMatcher{ + pos: position{line: 386, col: 19, offset: 8144}, + val: "=", + ignoreCase: false, + }, + &ruleRefExpr{ + pos: position{line: 386, col: 23, offset: 8148}, + name: "_", + }, + &labeledExpr{ + pos: position{line: 386, col: 25, offset: 8150}, + label: "e", + expr: &ruleRefExpr{ + pos: position{line: 386, col: 27, offset: 8152}, + name: "ExprWithDefault", + }, + }, + }, + }, + }, + }, + { + name: "UnaryOperator", + pos: position{line: 390, col: 1, offset: 8255}, + expr: &ruleRefExpr{ + pos: position{line: 391, col: 4, offset: 8272}, + name: "SignOperator", + }, + }, + { + name: "SignOperator", + pos: position{line: 393, col: 1, offset: 8286}, + expr: &actionExpr{ + pos: position{line: 394, col: 4, offset: 8302}, + run: (*parser).callonSignOperator1, + expr: &ruleRefExpr{ + pos: position{line: 394, col: 4, offset: 8302}, + name: "Sign", + }, + }, + }, + { + name: "NotOperator", + pos: position{line: 405, col: 1, offset: 8468}, + expr: &actionExpr{ + pos: position{line: 406, col: 4, offset: 8483}, + run: (*parser).callonNotOperator1, + expr: &ruleRefExpr{ + pos: position{line: 406, col: 4, offset: 8483}, + name: "NotToken", + }, + }, + }, + { + name: "AndOperator", + pos: position{line: 409, col: 1, offset: 8528}, + expr: &actionExpr{ + pos: position{line: 410, col: 4, offset: 8543}, + run: (*parser).callonAndOperator1, + expr: &ruleRefExpr{ + pos: position{line: 410, col: 4, offset: 8543}, + name: "AndToken", + }, + }, + }, + { + name: "OrOperator", + pos: position{line: 413, col: 1, offset: 8588}, + expr: &actionExpr{ + pos: position{line: 414, col: 4, offset: 8602}, + run: (*parser).callonOrOperator1, + expr: &ruleRefExpr{ + pos: position{line: 414, col: 4, offset: 8602}, + name: "OrToken", + }, + }, + }, + { + name: "CmpOperator", + pos: position{line: 417, col: 1, offset: 8645}, + expr: &actionExpr{ + pos: position{line: 418, col: 4, offset: 8660}, + run: (*parser).callonCmpOperator1, + expr: &choiceExpr{ + pos: position{line: 418, col: 6, offset: 8662}, + alternatives: []interface{}{ + &litMatcher{ + pos: position{line: 418, col: 6, offset: 8662}, + val: "<=", + ignoreCase: false, + }, + &litMatcher{ + pos: position{line: 418, col: 13, offset: 8669}, + val: ">=", + ignoreCase: false, + }, + &litMatcher{ + pos: position{line: 418, col: 20, offset: 8676}, + val: "<>", + ignoreCase: false, + }, + &litMatcher{ + pos: position{line: 418, col: 27, offset: 8683}, + val: "!=", + ignoreCase: false, + }, + &charClassMatcher{ + pos: position{line: 418, col: 34, offset: 8690}, + val: "[<>=]", + chars: []rune{'<', '>', '='}, + ignoreCase: false, + inverted: false, + }, + }, + }, + }, + }, + { + name: "ConcatOperator", + pos: position{line: 439, col: 1, offset: 9118}, + expr: &actionExpr{ + pos: position{line: 440, col: 4, offset: 9136}, + run: (*parser).callonConcatOperator1, + expr: &litMatcher{ + pos: position{line: 440, col: 4, offset: 9136}, + val: "||", + ignoreCase: false, + }, + }, + }, + { + name: "AddSubOperator", + pos: position{line: 443, col: 1, offset: 9180}, + expr: &actionExpr{ + pos: position{line: 444, col: 4, offset: 9198}, + run: (*parser).callonAddSubOperator1, + expr: &charClassMatcher{ + pos: position{line: 444, col: 4, offset: 9198}, + val: "[+-]", + chars: []rune{'+', '-'}, + ignoreCase: false, + inverted: false, + }, + }, + }, + { + name: "MulDivModOperator", + pos: position{line: 455, col: 1, offset: 9367}, + expr: &actionExpr{ + pos: position{line: 456, col: 4, offset: 9388}, + run: (*parser).callonMulDivModOperator1, + expr: &charClassMatcher{ + pos: position{line: 456, col: 4, offset: 9388}, + val: "[*/%]", + chars: []rune{'*', '/', '%'}, + ignoreCase: false, + inverted: false, + }, + }, + }, + { + name: "DataType", + pos: position{line: 470, col: 1, offset: 9617}, + expr: &choiceExpr{ + pos: position{line: 471, col: 4, offset: 9629}, + alternatives: []interface{}{ + &ruleRefExpr{ + pos: position{line: 471, col: 4, offset: 9629}, + name: "UIntType", + }, + &ruleRefExpr{ + pos: position{line: 472, col: 4, offset: 9641}, + name: "IntType", + }, + &ruleRefExpr{ + pos: position{line: 473, col: 4, offset: 9652}, + name: "UFixedType", + }, + &ruleRefExpr{ + pos: position{line: 474, col: 4, offset: 9666}, + name: "FixedType", + }, + &ruleRefExpr{ + pos: position{line: 475, col: 4, offset: 9679}, + name: "FixedBytesType", + }, + &ruleRefExpr{ + pos: position{line: 476, col: 4, offset: 9697}, + name: "DynamicBytesType", + }, + &ruleRefExpr{ + pos: position{line: 477, col: 4, offset: 9717}, + name: "BoolType", + }, + &ruleRefExpr{ + pos: position{line: 478, col: 4, offset: 9729}, + name: "AddressType", + }, + }, + }, + }, + { + name: "UIntType", + pos: position{line: 480, col: 1, offset: 9742}, + expr: &actionExpr{ + pos: position{line: 481, col: 4, offset: 9754}, + run: (*parser).callonUIntType1, + expr: &seqExpr{ + pos: position{line: 481, col: 4, offset: 9754}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 481, col: 4, offset: 9754}, + val: "uint", + ignoreCase: true, + }, + &labeledExpr{ + pos: position{line: 481, col: 12, offset: 9762}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 481, col: 14, offset: 9764}, + name: "NonZeroLeadingInteger", + }, + }, + ¬Expr{ + pos: position{line: 481, col: 36, offset: 9786}, + expr: &ruleRefExpr{ + pos: position{line: 481, col: 37, offset: 9787}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + }, + { + name: "IntType", + pos: position{line: 484, col: 1, offset: 9878}, + expr: &actionExpr{ + pos: position{line: 485, col: 4, offset: 9889}, + run: (*parser).callonIntType1, + expr: &seqExpr{ + pos: position{line: 485, col: 4, offset: 9889}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 485, col: 4, offset: 9889}, + val: "int", + ignoreCase: true, + }, + &labeledExpr{ + pos: position{line: 485, col: 11, offset: 9896}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 485, col: 13, offset: 9898}, + name: "NonZeroLeadingInteger", + }, + }, + ¬Expr{ + pos: position{line: 485, col: 35, offset: 9920}, + expr: &ruleRefExpr{ + pos: position{line: 485, col: 36, offset: 9921}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + }, + { + name: "UFixedType", + pos: position{line: 488, col: 1, offset: 9996}, + expr: &actionExpr{ + pos: position{line: 489, col: 4, offset: 10010}, + run: (*parser).callonUFixedType1, + expr: &seqExpr{ + pos: position{line: 489, col: 4, offset: 10010}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 489, col: 4, offset: 10010}, + val: "ufixed", + ignoreCase: true, + }, + &labeledExpr{ + pos: position{line: 489, col: 14, offset: 10020}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 489, col: 16, offset: 10022}, + name: "NonZeroLeadingInteger", + }, + }, + &litMatcher{ + pos: position{line: 489, col: 38, offset: 10044}, + val: "x", + ignoreCase: true, + }, + &labeledExpr{ + pos: position{line: 489, col: 43, offset: 10049}, + label: "t", + expr: &ruleRefExpr{ + pos: position{line: 489, col: 45, offset: 10051}, + name: "NonZeroLeadingInteger", + }, + }, + ¬Expr{ + pos: position{line: 489, col: 67, offset: 10073}, + expr: &ruleRefExpr{ + pos: position{line: 489, col: 68, offset: 10074}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + }, + { + name: "FixedType", + pos: position{line: 498, col: 1, offset: 10229}, + expr: &actionExpr{ + pos: position{line: 499, col: 4, offset: 10242}, + run: (*parser).callonFixedType1, + expr: &seqExpr{ + pos: position{line: 499, col: 4, offset: 10242}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 499, col: 4, offset: 10242}, + val: "fixed", + ignoreCase: true, + }, + &labeledExpr{ + pos: position{line: 499, col: 13, offset: 10251}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 499, col: 15, offset: 10253}, + name: "NonZeroLeadingInteger", + }, + }, + &litMatcher{ + pos: position{line: 499, col: 37, offset: 10275}, + val: "x", + ignoreCase: true, + }, + &labeledExpr{ + pos: position{line: 499, col: 42, offset: 10280}, + label: "t", + expr: &ruleRefExpr{ + pos: position{line: 499, col: 44, offset: 10282}, + name: "NonZeroLeadingInteger", + }, + }, + ¬Expr{ + pos: position{line: 499, col: 66, offset: 10304}, + expr: &ruleRefExpr{ + pos: position{line: 499, col: 67, offset: 10305}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + }, + { + name: "FixedBytesType", + pos: position{line: 507, col: 1, offset: 10436}, + expr: &choiceExpr{ + pos: position{line: 508, col: 4, offset: 10454}, + alternatives: []interface{}{ + &actionExpr{ + pos: position{line: 508, col: 4, offset: 10454}, + run: (*parser).callonFixedBytesType2, + expr: &seqExpr{ + pos: position{line: 508, col: 4, offset: 10454}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 508, col: 4, offset: 10454}, + val: "bytes", + ignoreCase: true, + }, + &labeledExpr{ + pos: position{line: 508, col: 13, offset: 10463}, + label: "s", + expr: &ruleRefExpr{ + pos: position{line: 508, col: 15, offset: 10465}, + name: "NonZeroLeadingInteger", + }, + }, + ¬Expr{ + pos: position{line: 508, col: 37, offset: 10487}, + expr: &ruleRefExpr{ + pos: position{line: 508, col: 38, offset: 10488}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + &actionExpr{ + pos: position{line: 510, col: 4, offset: 10572}, + run: (*parser).callonFixedBytesType9, + expr: &seqExpr{ + pos: position{line: 510, col: 4, offset: 10572}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 510, col: 4, offset: 10572}, + val: "byte", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 510, col: 12, offset: 10580}, + expr: &ruleRefExpr{ + pos: position{line: 510, col: 13, offset: 10581}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "DynamicBytesType", + pos: position{line: 513, col: 1, offset: 10647}, + expr: &actionExpr{ + pos: position{line: 514, col: 4, offset: 10667}, + run: (*parser).callonDynamicBytesType1, + expr: &choiceExpr{ + pos: position{line: 514, col: 6, offset: 10669}, + alternatives: []interface{}{ + &seqExpr{ + pos: position{line: 514, col: 6, offset: 10669}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 514, col: 6, offset: 10669}, + val: "bytes", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 514, col: 15, offset: 10678}, + expr: &ruleRefExpr{ + pos: position{line: 514, col: 16, offset: 10679}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + &seqExpr{ + pos: position{line: 515, col: 5, offset: 10704}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 515, col: 5, offset: 10704}, + val: "string", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 515, col: 15, offset: 10714}, + expr: &ruleRefExpr{ + pos: position{line: 515, col: 16, offset: 10715}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + &seqExpr{ + pos: position{line: 516, col: 5, offset: 10740}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 516, col: 5, offset: 10740}, + val: "text", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 516, col: 13, offset: 10748}, + expr: &ruleRefExpr{ + pos: position{line: 516, col: 14, offset: 10749}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "AddressType", + pos: position{line: 520, col: 1, offset: 10813}, + expr: &actionExpr{ + pos: position{line: 521, col: 4, offset: 10828}, + run: (*parser).callonAddressType1, + expr: &seqExpr{ + pos: position{line: 521, col: 4, offset: 10828}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 521, col: 4, offset: 10828}, + val: "address", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 521, col: 15, offset: 10839}, + expr: &ruleRefExpr{ + pos: position{line: 521, col: 16, offset: 10840}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + }, + { + name: "BoolType", + pos: position{line: 524, col: 1, offset: 10896}, + expr: &actionExpr{ + pos: position{line: 525, col: 4, offset: 10908}, + run: (*parser).callonBoolType1, + expr: &choiceExpr{ + pos: position{line: 525, col: 6, offset: 10910}, + alternatives: []interface{}{ + &seqExpr{ + pos: position{line: 525, col: 6, offset: 10910}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 525, col: 6, offset: 10910}, + val: "bool", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 525, col: 14, offset: 10918}, + expr: &ruleRefExpr{ + pos: position{line: 525, col: 15, offset: 10919}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + &seqExpr{ + pos: position{line: 526, col: 5, offset: 10944}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 526, col: 5, offset: 10944}, + val: "boolean", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 526, col: 16, offset: 10955}, + expr: &ruleRefExpr{ + pos: position{line: 526, col: 17, offset: 10956}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "Value", + pos: position{line: 531, col: 1, offset: 11024}, + expr: &choiceExpr{ + pos: position{line: 532, col: 4, offset: 11033}, + alternatives: []interface{}{ + &ruleRefExpr{ + pos: position{line: 532, col: 4, offset: 11033}, + name: "NumberLiteral", + }, + &ruleRefExpr{ + pos: position{line: 533, col: 4, offset: 11050}, + name: "StringLiteral", + }, + &ruleRefExpr{ + pos: position{line: 534, col: 4, offset: 11067}, + name: "BoolLiteral", + }, + &ruleRefExpr{ + pos: position{line: 535, col: 4, offset: 11082}, + name: "NullLiteral", + }, + }, + }, + }, + { + name: "AnyLiteral", + pos: position{line: 537, col: 1, offset: 11095}, + expr: &actionExpr{ + pos: position{line: 538, col: 4, offset: 11109}, + run: (*parser).callonAnyLiteral1, + expr: &ruleRefExpr{ + pos: position{line: 538, col: 4, offset: 11109}, + name: "AnyToken", + }, + }, + }, + { + name: "DefaultLiteral", + pos: position{line: 541, col: 1, offset: 11150}, + expr: &actionExpr{ + pos: position{line: 542, col: 4, offset: 11168}, + run: (*parser).callonDefaultLiteral1, + expr: &ruleRefExpr{ + pos: position{line: 542, col: 4, offset: 11168}, + name: "DefaultToken", + }, + }, + }, + { + name: "BoolLiteral", + pos: position{line: 545, col: 1, offset: 11217}, + expr: &actionExpr{ + pos: position{line: 546, col: 4, offset: 11232}, + run: (*parser).callonBoolLiteral1, + expr: &labeledExpr{ + pos: position{line: 546, col: 4, offset: 11232}, + label: "b", + expr: &choiceExpr{ + pos: position{line: 546, col: 8, offset: 11236}, + alternatives: []interface{}{ + &ruleRefExpr{ + pos: position{line: 546, col: 8, offset: 11236}, + name: "TrueToken", + }, + &ruleRefExpr{ + pos: position{line: 546, col: 20, offset: 11248}, + name: "FalseToken", + }, + }, + }, + }, + }, + }, + { + name: "NullLiteral", + pos: position{line: 549, col: 1, offset: 11322}, + expr: &actionExpr{ + pos: position{line: 550, col: 4, offset: 11337}, + run: (*parser).callonNullLiteral1, + expr: &ruleRefExpr{ + pos: position{line: 550, col: 4, offset: 11337}, + name: "NullToken", + }, + }, + }, + { + name: "NumberLiteral", + pos: position{line: 553, col: 1, offset: 11380}, + expr: &choiceExpr{ + pos: position{line: 554, col: 4, offset: 11397}, + alternatives: []interface{}{ + &actionExpr{ + pos: position{line: 554, col: 4, offset: 11397}, + run: (*parser).callonNumberLiteral2, + expr: &seqExpr{ + pos: position{line: 554, col: 4, offset: 11397}, + exprs: []interface{}{ + &andExpr{ + pos: position{line: 554, col: 4, offset: 11397}, + expr: &seqExpr{ + pos: position{line: 554, col: 6, offset: 11399}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 554, col: 6, offset: 11399}, + val: "0", + ignoreCase: false, + }, + &litMatcher{ + pos: position{line: 554, col: 10, offset: 11403}, + val: "x", + ignoreCase: true, + }, + }, + }, + }, + &labeledExpr{ + pos: position{line: 554, col: 16, offset: 11409}, + label: "h", + expr: &ruleRefExpr{ + pos: position{line: 554, col: 18, offset: 11411}, + name: "Hex", + }, + }, + }, + }, + }, + &ruleRefExpr{ + pos: position{line: 555, col: 4, offset: 11436}, + name: "Decimal", + }, + }, + }, + }, + { + name: "Sign", + pos: position{line: 557, col: 1, offset: 11445}, + expr: &charClassMatcher{ + pos: position{line: 558, col: 4, offset: 11453}, + val: "[-+]", + chars: []rune{'-', '+'}, + ignoreCase: false, + inverted: false, + }, + }, + { + name: "Integer", + pos: position{line: 560, col: 1, offset: 11459}, + expr: &actionExpr{ + pos: position{line: 561, col: 4, offset: 11470}, + run: (*parser).callonInteger1, + expr: &oneOrMoreExpr{ + pos: position{line: 561, col: 4, offset: 11470}, + expr: &charClassMatcher{ + pos: position{line: 561, col: 4, offset: 11470}, + val: "[0-9]", + ranges: []rune{'0', '9'}, + ignoreCase: false, + inverted: false, + }, + }, + }, + }, + { + name: "NonZeroLeadingInteger", + pos: position{line: 564, col: 1, offset: 11533}, + expr: &actionExpr{ + pos: position{line: 565, col: 4, offset: 11558}, + run: (*parser).callonNonZeroLeadingInteger1, + expr: &choiceExpr{ + pos: position{line: 565, col: 6, offset: 11560}, + alternatives: []interface{}{ + &litMatcher{ + pos: position{line: 565, col: 6, offset: 11560}, + val: "0", + ignoreCase: false, + }, + &seqExpr{ + pos: position{line: 565, col: 12, offset: 11566}, + exprs: []interface{}{ + &charClassMatcher{ + pos: position{line: 565, col: 12, offset: 11566}, + val: "[1-9]", + ranges: []rune{'1', '9'}, + ignoreCase: false, + inverted: false, + }, + &zeroOrMoreExpr{ + pos: position{line: 565, col: 17, offset: 11571}, + expr: &charClassMatcher{ + pos: position{line: 565, col: 17, offset: 11571}, + val: "[0-9]", + ranges: []rune{'0', '9'}, + ignoreCase: false, + inverted: false, + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "Fixnum", + pos: position{line: 568, col: 1, offset: 11604}, + expr: &choiceExpr{ + pos: position{line: 569, col: 4, offset: 11614}, + alternatives: []interface{}{ + &seqExpr{ + pos: position{line: 569, col: 4, offset: 11614}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 569, col: 4, offset: 11614}, + name: "Integer", + }, + &litMatcher{ + pos: position{line: 569, col: 12, offset: 11622}, + val: ".", + ignoreCase: false, + }, + &ruleRefExpr{ + pos: position{line: 569, col: 16, offset: 11626}, + name: "Integer", + }, + }, + }, + &seqExpr{ + pos: position{line: 570, col: 4, offset: 11637}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 570, col: 4, offset: 11637}, + name: "Integer", + }, + &zeroOrOneExpr{ + pos: position{line: 570, col: 12, offset: 11645}, + expr: &litMatcher{ + pos: position{line: 570, col: 12, offset: 11645}, + val: ".", + ignoreCase: false, + }, + }, + }, + }, + &seqExpr{ + pos: position{line: 571, col: 4, offset: 11653}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 571, col: 4, offset: 11653}, + val: ".", + ignoreCase: false, + }, + &ruleRefExpr{ + pos: position{line: 571, col: 8, offset: 11657}, + name: "Integer", + }, + }, + }, + }, + }, + }, + { + name: "Decimal", + pos: position{line: 573, col: 1, offset: 11666}, + expr: &actionExpr{ + pos: position{line: 574, col: 4, offset: 11677}, + run: (*parser).callonDecimal1, + expr: &seqExpr{ + pos: position{line: 574, col: 4, offset: 11677}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 574, col: 4, offset: 11677}, + name: "Fixnum", + }, + &zeroOrOneExpr{ + pos: position{line: 574, col: 11, offset: 11684}, + expr: &seqExpr{ + pos: position{line: 574, col: 13, offset: 11686}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 574, col: 13, offset: 11686}, + val: "e", + ignoreCase: true, + }, + &zeroOrOneExpr{ + pos: position{line: 574, col: 18, offset: 11691}, + expr: &ruleRefExpr{ + pos: position{line: 574, col: 18, offset: 11691}, + name: "Sign", + }, + }, + &ruleRefExpr{ + pos: position{line: 574, col: 24, offset: 11697}, + name: "Integer", + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "Hex", + pos: position{line: 577, col: 1, offset: 11761}, + expr: &actionExpr{ + pos: position{line: 578, col: 4, offset: 11768}, + run: (*parser).callonHex1, + expr: &seqExpr{ + pos: position{line: 578, col: 4, offset: 11768}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 578, col: 4, offset: 11768}, + val: "0", + ignoreCase: false, + }, + &litMatcher{ + pos: position{line: 578, col: 8, offset: 11772}, + val: "x", + ignoreCase: true, + }, + &labeledExpr{ + pos: position{line: 578, col: 13, offset: 11777}, + label: "s", + expr: &oneOrMoreExpr{ + pos: position{line: 578, col: 15, offset: 11779}, + expr: &charClassMatcher{ + pos: position{line: 578, col: 17, offset: 11781}, + val: "[0-9A-Fa-f]", + ranges: []rune{'0', '9', 'A', 'F', 'a', 'f'}, + ignoreCase: false, + inverted: false, + }, + }, + }, + ¬Expr{ + pos: position{line: 578, col: 32, offset: 11796}, + expr: &ruleRefExpr{ + pos: position{line: 578, col: 33, offset: 11797}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + }, + { + name: "StringLiteral", + pos: position{line: 581, col: 1, offset: 11862}, + expr: &choiceExpr{ + pos: position{line: 582, col: 4, offset: 11879}, + alternatives: []interface{}{ + &ruleRefExpr{ + pos: position{line: 582, col: 4, offset: 11879}, + name: "HexString", + }, + &ruleRefExpr{ + pos: position{line: 583, col: 4, offset: 11892}, + name: "NormalString", + }, + }, + }, + }, + { + name: "HexString", + pos: position{line: 585, col: 1, offset: 11906}, + expr: &actionExpr{ + pos: position{line: 586, col: 4, offset: 11919}, + run: (*parser).callonHexString1, + expr: &seqExpr{ + pos: position{line: 586, col: 4, offset: 11919}, + exprs: []interface{}{ + &choiceExpr{ + pos: position{line: 586, col: 6, offset: 11921}, + alternatives: []interface{}{ + &litMatcher{ + pos: position{line: 586, col: 6, offset: 11921}, + val: "hex", + ignoreCase: true, + }, + &litMatcher{ + pos: position{line: 586, col: 15, offset: 11930}, + val: "x", + ignoreCase: true, + }, + }, + }, + &litMatcher{ + pos: position{line: 586, col: 22, offset: 11937}, + val: "'", + ignoreCase: false, + }, + &labeledExpr{ + pos: position{line: 586, col: 26, offset: 11941}, + label: "s", + expr: &zeroOrMoreExpr{ + pos: position{line: 586, col: 28, offset: 11943}, + expr: &actionExpr{ + pos: position{line: 586, col: 29, offset: 11944}, + run: (*parser).callonHexString9, + expr: &seqExpr{ + pos: position{line: 586, col: 29, offset: 11944}, + exprs: []interface{}{ + &charClassMatcher{ + pos: position{line: 586, col: 29, offset: 11944}, + val: "[0-9a-fA-F]", + ranges: []rune{'0', '9', 'a', 'f', 'A', 'F'}, + ignoreCase: false, + inverted: false, + }, + &charClassMatcher{ + pos: position{line: 586, col: 40, offset: 11955}, + val: "[0-9a-fA-F]", + ranges: []rune{'0', '9', 'a', 'f', 'A', 'F'}, + ignoreCase: false, + inverted: false, + }, + }, + }, + }, + }, + }, + &litMatcher{ + pos: position{line: 586, col: 78, offset: 11993}, + val: "'", + ignoreCase: false, + }, + }, + }, + }, + }, + { + name: "NormalString", + pos: position{line: 589, col: 1, offset: 12055}, + expr: &actionExpr{ + pos: position{line: 590, col: 4, offset: 12071}, + run: (*parser).callonNormalString1, + expr: &seqExpr{ + pos: position{line: 590, col: 4, offset: 12071}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 590, col: 4, offset: 12071}, + val: "'", + ignoreCase: false, + }, + &labeledExpr{ + pos: position{line: 590, col: 8, offset: 12075}, + label: "s", + expr: &zeroOrMoreExpr{ + pos: position{line: 590, col: 10, offset: 12077}, + expr: &actionExpr{ + pos: position{line: 590, col: 12, offset: 12079}, + run: (*parser).callonNormalString6, + expr: &choiceExpr{ + pos: position{line: 590, col: 14, offset: 12081}, + alternatives: []interface{}{ + &charClassMatcher{ + pos: position{line: 590, col: 14, offset: 12081}, + val: "[^'\\r\\n\\\\]", + chars: []rune{'\'', '\r', '\n', '\\'}, + ignoreCase: false, + inverted: true, + }, + &seqExpr{ + pos: position{line: 590, col: 27, offset: 12094}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 590, col: 27, offset: 12094}, + val: "\\", + ignoreCase: false, + }, + &anyMatcher{ + line: 590, col: 32, offset: 12099, + }, + }, + }, + }, + }, + }, + }, + }, + &litMatcher{ + pos: position{line: 590, col: 62, offset: 12129}, + val: "'", + ignoreCase: false, + }, + }, + }, + }, + }, + { + name: "SelectToken", + pos: position{line: 594, col: 1, offset: 12206}, + expr: &seqExpr{ + pos: position{line: 595, col: 4, offset: 12221}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 595, col: 4, offset: 12221}, + val: "select", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 595, col: 14, offset: 12231}, + expr: &ruleRefExpr{ + pos: position{line: 595, col: 15, offset: 12232}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "FromToken", + pos: position{line: 597, col: 1, offset: 12254}, + expr: &seqExpr{ + pos: position{line: 598, col: 4, offset: 12267}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 598, col: 4, offset: 12267}, + val: "from", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 598, col: 12, offset: 12275}, + expr: &ruleRefExpr{ + pos: position{line: 598, col: 13, offset: 12276}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "WhereToken", + pos: position{line: 600, col: 1, offset: 12298}, + expr: &seqExpr{ + pos: position{line: 601, col: 4, offset: 12312}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 601, col: 4, offset: 12312}, + val: "where", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 601, col: 13, offset: 12321}, + expr: &ruleRefExpr{ + pos: position{line: 601, col: 14, offset: 12322}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "OrderToken", + pos: position{line: 603, col: 1, offset: 12344}, + expr: &seqExpr{ + pos: position{line: 604, col: 4, offset: 12358}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 604, col: 4, offset: 12358}, + val: "order", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 604, col: 13, offset: 12367}, + expr: &ruleRefExpr{ + pos: position{line: 604, col: 14, offset: 12368}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "ByToken", + pos: position{line: 606, col: 1, offset: 12390}, + expr: &seqExpr{ + pos: position{line: 607, col: 4, offset: 12401}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 607, col: 4, offset: 12401}, + val: "by", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 607, col: 10, offset: 12407}, + expr: &ruleRefExpr{ + pos: position{line: 607, col: 11, offset: 12408}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "GroupToken", + pos: position{line: 609, col: 1, offset: 12430}, + expr: &seqExpr{ + pos: position{line: 610, col: 4, offset: 12444}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 610, col: 4, offset: 12444}, + val: "group", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 610, col: 13, offset: 12453}, + expr: &ruleRefExpr{ + pos: position{line: 610, col: 14, offset: 12454}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "LimitToken", + pos: position{line: 612, col: 1, offset: 12476}, + expr: &seqExpr{ + pos: position{line: 613, col: 4, offset: 12490}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 613, col: 4, offset: 12490}, + val: "limit", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 613, col: 13, offset: 12499}, + expr: &ruleRefExpr{ + pos: position{line: 613, col: 14, offset: 12500}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "OffsetToken", + pos: position{line: 615, col: 1, offset: 12522}, + expr: &seqExpr{ + pos: position{line: 616, col: 4, offset: 12537}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 616, col: 4, offset: 12537}, + val: "offset", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 616, col: 14, offset: 12547}, + expr: &ruleRefExpr{ + pos: position{line: 616, col: 15, offset: 12548}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "UpdateToken", + pos: position{line: 618, col: 1, offset: 12570}, + expr: &seqExpr{ + pos: position{line: 619, col: 4, offset: 12585}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 619, col: 4, offset: 12585}, + val: "update", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 619, col: 14, offset: 12595}, + expr: &ruleRefExpr{ + pos: position{line: 619, col: 15, offset: 12596}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "SetToken", + pos: position{line: 621, col: 1, offset: 12618}, + expr: &seqExpr{ + pos: position{line: 622, col: 4, offset: 12630}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 622, col: 4, offset: 12630}, + val: "set", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 622, col: 11, offset: 12637}, + expr: &ruleRefExpr{ + pos: position{line: 622, col: 12, offset: 12638}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "DeleteToken", + pos: position{line: 624, col: 1, offset: 12660}, + expr: &seqExpr{ + pos: position{line: 625, col: 4, offset: 12675}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 625, col: 4, offset: 12675}, + val: "delete", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 625, col: 14, offset: 12685}, + expr: &ruleRefExpr{ + pos: position{line: 625, col: 15, offset: 12686}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "InsertToken", + pos: position{line: 627, col: 1, offset: 12708}, + expr: &seqExpr{ + pos: position{line: 628, col: 4, offset: 12723}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 628, col: 4, offset: 12723}, + val: "insert", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 628, col: 14, offset: 12733}, + expr: &ruleRefExpr{ + pos: position{line: 628, col: 15, offset: 12734}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "IntoToken", + pos: position{line: 630, col: 1, offset: 12756}, + expr: &seqExpr{ + pos: position{line: 631, col: 4, offset: 12769}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 631, col: 4, offset: 12769}, + val: "into", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 631, col: 12, offset: 12777}, + expr: &ruleRefExpr{ + pos: position{line: 631, col: 13, offset: 12778}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "ValuesToken", + pos: position{line: 633, col: 1, offset: 12800}, + expr: &seqExpr{ + pos: position{line: 634, col: 4, offset: 12815}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 634, col: 4, offset: 12815}, + val: "values", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 634, col: 14, offset: 12825}, + expr: &ruleRefExpr{ + pos: position{line: 634, col: 15, offset: 12826}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "CreateToken", + pos: position{line: 636, col: 1, offset: 12848}, + expr: &seqExpr{ + pos: position{line: 637, col: 4, offset: 12863}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 637, col: 4, offset: 12863}, + val: "create", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 637, col: 14, offset: 12873}, + expr: &ruleRefExpr{ + pos: position{line: 637, col: 15, offset: 12874}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "TableToken", + pos: position{line: 639, col: 1, offset: 12896}, + expr: &seqExpr{ + pos: position{line: 640, col: 4, offset: 12910}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 640, col: 4, offset: 12910}, + val: "table", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 640, col: 13, offset: 12919}, + expr: &ruleRefExpr{ + pos: position{line: 640, col: 14, offset: 12920}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "IndexToken", + pos: position{line: 642, col: 1, offset: 12942}, + expr: &seqExpr{ + pos: position{line: 643, col: 4, offset: 12956}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 643, col: 4, offset: 12956}, + val: "index", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 643, col: 13, offset: 12965}, + expr: &ruleRefExpr{ + pos: position{line: 643, col: 14, offset: 12966}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "UniqueToken", + pos: position{line: 645, col: 1, offset: 12988}, + expr: &seqExpr{ + pos: position{line: 646, col: 4, offset: 13003}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 646, col: 4, offset: 13003}, + val: "unique", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 646, col: 14, offset: 13013}, + expr: &ruleRefExpr{ + pos: position{line: 646, col: 15, offset: 13014}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "DefaultToken", + pos: position{line: 648, col: 1, offset: 13036}, + expr: &seqExpr{ + pos: position{line: 649, col: 4, offset: 13052}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 649, col: 4, offset: 13052}, + val: "default", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 649, col: 15, offset: 13063}, + expr: &ruleRefExpr{ + pos: position{line: 649, col: 16, offset: 13064}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "PrimaryToken", + pos: position{line: 651, col: 1, offset: 13086}, + expr: &seqExpr{ + pos: position{line: 652, col: 4, offset: 13102}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 652, col: 4, offset: 13102}, + val: "primary", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 652, col: 15, offset: 13113}, + expr: &ruleRefExpr{ + pos: position{line: 652, col: 16, offset: 13114}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "KeyToken", + pos: position{line: 654, col: 1, offset: 13136}, + expr: &seqExpr{ + pos: position{line: 655, col: 4, offset: 13148}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 655, col: 4, offset: 13148}, + val: "key", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 655, col: 11, offset: 13155}, + expr: &ruleRefExpr{ + pos: position{line: 655, col: 12, offset: 13156}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "ReferencesToken", + pos: position{line: 657, col: 1, offset: 13178}, + expr: &seqExpr{ + pos: position{line: 658, col: 4, offset: 13197}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 658, col: 4, offset: 13197}, + val: "references", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 658, col: 18, offset: 13211}, + expr: &ruleRefExpr{ + pos: position{line: 658, col: 19, offset: 13212}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "AutoincrementToken", + pos: position{line: 660, col: 1, offset: 13234}, + expr: &seqExpr{ + pos: position{line: 661, col: 4, offset: 13256}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 661, col: 4, offset: 13256}, + val: "autoincrement", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 661, col: 21, offset: 13273}, + expr: &ruleRefExpr{ + pos: position{line: 661, col: 22, offset: 13274}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "OnToken", + pos: position{line: 663, col: 1, offset: 13296}, + expr: &seqExpr{ + pos: position{line: 664, col: 4, offset: 13307}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 664, col: 4, offset: 13307}, + val: "on", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 664, col: 10, offset: 13313}, + expr: &ruleRefExpr{ + pos: position{line: 664, col: 11, offset: 13314}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "TrueToken", + pos: position{line: 666, col: 1, offset: 13336}, + expr: &actionExpr{ + pos: position{line: 667, col: 4, offset: 13349}, + run: (*parser).callonTrueToken1, + expr: &seqExpr{ + pos: position{line: 667, col: 4, offset: 13349}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 667, col: 4, offset: 13349}, + val: "true", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 667, col: 12, offset: 13357}, + expr: &ruleRefExpr{ + pos: position{line: 667, col: 13, offset: 13358}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + }, + { + name: "FalseToken", + pos: position{line: 670, col: 1, offset: 13412}, + expr: &actionExpr{ + pos: position{line: 671, col: 4, offset: 13426}, + run: (*parser).callonFalseToken1, + expr: &seqExpr{ + pos: position{line: 671, col: 4, offset: 13426}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 671, col: 4, offset: 13426}, + val: "false", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 671, col: 13, offset: 13435}, + expr: &ruleRefExpr{ + pos: position{line: 671, col: 14, offset: 13436}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + }, + { + name: "NullToken", + pos: position{line: 674, col: 1, offset: 13490}, + expr: &seqExpr{ + pos: position{line: 675, col: 4, offset: 13503}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 675, col: 4, offset: 13503}, + val: "null", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 675, col: 12, offset: 13511}, + expr: &ruleRefExpr{ + pos: position{line: 675, col: 13, offset: 13512}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "IsToken", + pos: position{line: 677, col: 1, offset: 13534}, + expr: &seqExpr{ + pos: position{line: 678, col: 4, offset: 13545}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 678, col: 4, offset: 13545}, + val: "is", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 678, col: 10, offset: 13551}, + expr: &ruleRefExpr{ + pos: position{line: 678, col: 11, offset: 13552}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "NullsToken", + pos: position{line: 680, col: 1, offset: 13574}, + expr: &seqExpr{ + pos: position{line: 681, col: 4, offset: 13588}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 681, col: 4, offset: 13588}, + val: "nulls", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 681, col: 13, offset: 13597}, + expr: &ruleRefExpr{ + pos: position{line: 681, col: 14, offset: 13598}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "LastToken", + pos: position{line: 683, col: 1, offset: 13620}, + expr: &actionExpr{ + pos: position{line: 684, col: 4, offset: 13633}, + run: (*parser).callonLastToken1, + expr: &seqExpr{ + pos: position{line: 684, col: 4, offset: 13633}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 684, col: 4, offset: 13633}, + val: "last", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 684, col: 12, offset: 13641}, + expr: &ruleRefExpr{ + pos: position{line: 684, col: 13, offset: 13642}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + }, + { + name: "FirstToken", + pos: position{line: 687, col: 1, offset: 13696}, + expr: &actionExpr{ + pos: position{line: 688, col: 4, offset: 13710}, + run: (*parser).callonFirstToken1, + expr: &seqExpr{ + pos: position{line: 688, col: 4, offset: 13710}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 688, col: 4, offset: 13710}, + val: "first", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 688, col: 13, offset: 13719}, + expr: &ruleRefExpr{ + pos: position{line: 688, col: 14, offset: 13720}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + }, + { + name: "AndToken", + pos: position{line: 691, col: 1, offset: 13774}, + expr: &seqExpr{ + pos: position{line: 692, col: 4, offset: 13786}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 692, col: 4, offset: 13786}, + val: "and", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 692, col: 11, offset: 13793}, + expr: &ruleRefExpr{ + pos: position{line: 692, col: 12, offset: 13794}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "OrToken", + pos: position{line: 694, col: 1, offset: 13816}, + expr: &seqExpr{ + pos: position{line: 695, col: 4, offset: 13827}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 695, col: 4, offset: 13827}, + val: "or", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 695, col: 10, offset: 13833}, + expr: &ruleRefExpr{ + pos: position{line: 695, col: 11, offset: 13834}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "NotToken", + pos: position{line: 697, col: 1, offset: 13856}, + expr: &seqExpr{ + pos: position{line: 698, col: 4, offset: 13868}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 698, col: 4, offset: 13868}, + val: "not", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 698, col: 11, offset: 13875}, + expr: &ruleRefExpr{ + pos: position{line: 698, col: 12, offset: 13876}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "InToken", + pos: position{line: 700, col: 1, offset: 13898}, + expr: &seqExpr{ + pos: position{line: 701, col: 4, offset: 13909}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 701, col: 4, offset: 13909}, + val: "in", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 701, col: 10, offset: 13915}, + expr: &ruleRefExpr{ + pos: position{line: 701, col: 11, offset: 13916}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "LikeToken", + pos: position{line: 703, col: 1, offset: 13938}, + expr: &seqExpr{ + pos: position{line: 704, col: 4, offset: 13951}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 704, col: 4, offset: 13951}, + val: "like", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 704, col: 12, offset: 13959}, + expr: &ruleRefExpr{ + pos: position{line: 704, col: 13, offset: 13960}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "AscToken", + pos: position{line: 706, col: 1, offset: 13982}, + expr: &actionExpr{ + pos: position{line: 707, col: 4, offset: 13994}, + run: (*parser).callonAscToken1, + expr: &seqExpr{ + pos: position{line: 707, col: 4, offset: 13994}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 707, col: 4, offset: 13994}, + val: "asc", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 707, col: 11, offset: 14001}, + expr: &ruleRefExpr{ + pos: position{line: 707, col: 12, offset: 14002}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + }, + { + name: "DescToken", + pos: position{line: 710, col: 1, offset: 14056}, + expr: &actionExpr{ + pos: position{line: 711, col: 4, offset: 14069}, + run: (*parser).callonDescToken1, + expr: &seqExpr{ + pos: position{line: 711, col: 4, offset: 14069}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 711, col: 4, offset: 14069}, + val: "desc", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 711, col: 12, offset: 14077}, + expr: &ruleRefExpr{ + pos: position{line: 711, col: 13, offset: 14078}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + }, + { + name: "CastToken", + pos: position{line: 714, col: 1, offset: 14132}, + expr: &seqExpr{ + pos: position{line: 715, col: 4, offset: 14145}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 715, col: 4, offset: 14145}, + val: "cast", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 715, col: 12, offset: 14153}, + expr: &ruleRefExpr{ + pos: position{line: 715, col: 13, offset: 14154}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "AsToken", + pos: position{line: 717, col: 1, offset: 14176}, + expr: &seqExpr{ + pos: position{line: 718, col: 4, offset: 14187}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 718, col: 4, offset: 14187}, + val: "as", + ignoreCase: true, + }, + ¬Expr{ + pos: position{line: 718, col: 10, offset: 14193}, + expr: &ruleRefExpr{ + pos: position{line: 718, col: 11, offset: 14194}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + { + name: "SeparatorToken", + pos: position{line: 720, col: 1, offset: 14216}, + expr: &litMatcher{ + pos: position{line: 721, col: 4, offset: 14234}, + val: ",", + ignoreCase: false, + }, + }, + { + name: "AnyToken", + pos: position{line: 723, col: 1, offset: 14239}, + expr: &litMatcher{ + pos: position{line: 724, col: 4, offset: 14251}, + val: "*", + ignoreCase: false, + }, + }, + { + name: "Identifier", + pos: position{line: 727, col: 1, offset: 14273}, + expr: &choiceExpr{ + pos: position{line: 728, col: 4, offset: 14287}, + alternatives: []interface{}{ + &ruleRefExpr{ + pos: position{line: 728, col: 4, offset: 14287}, + name: "NormalIdentifier", + }, + &ruleRefExpr{ + pos: position{line: 729, col: 4, offset: 14307}, + name: "StringIdentifier", + }, + }, + }, + }, + { + name: "NormalIdentifier", + pos: position{line: 731, col: 1, offset: 14325}, + expr: &actionExpr{ + pos: position{line: 732, col: 4, offset: 14345}, + run: (*parser).callonNormalIdentifier1, + expr: &seqExpr{ + pos: position{line: 732, col: 4, offset: 14345}, + exprs: []interface{}{ + &ruleRefExpr{ + pos: position{line: 732, col: 4, offset: 14345}, + name: "NormalIdentifierStart", + }, + &zeroOrMoreExpr{ + pos: position{line: 732, col: 26, offset: 14367}, + expr: &ruleRefExpr{ + pos: position{line: 732, col: 26, offset: 14367}, + name: "NormalIdentifierRest", + }, + }, + }, + }, + }, + }, + { + name: "NormalIdentifierStart", + pos: position{line: 735, col: 1, offset: 14429}, + expr: &charClassMatcher{ + pos: position{line: 736, col: 4, offset: 14454}, + val: "[a-zA-Z@#_\\u0080-\\uffff]", + chars: []rune{'@', '#', '_'}, + ranges: []rune{'a', 'z', 'A', 'Z', '\u0080', '\uffff'}, + ignoreCase: false, + inverted: false, + }, + }, + { + name: "NormalIdentifierRest", + pos: position{line: 738, col: 1, offset: 14480}, + expr: &charClassMatcher{ + pos: position{line: 739, col: 4, offset: 14504}, + val: "[a-zA-Z0-9@#$_\\u0080-\\uffff]", + chars: []rune{'@', '#', '$', '_'}, + ranges: []rune{'a', 'z', 'A', 'Z', '0', '9', '\u0080', '\uffff'}, + ignoreCase: false, + inverted: false, + }, + }, + { + name: "StringIdentifier", + pos: position{line: 741, col: 1, offset: 14534}, + expr: &actionExpr{ + pos: position{line: 742, col: 4, offset: 14554}, + run: (*parser).callonStringIdentifier1, + expr: &seqExpr{ + pos: position{line: 742, col: 4, offset: 14554}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 742, col: 4, offset: 14554}, + val: "\"", + ignoreCase: false, + }, + &labeledExpr{ + pos: position{line: 742, col: 9, offset: 14559}, + label: "s", + expr: &zeroOrMoreExpr{ + pos: position{line: 742, col: 11, offset: 14561}, + expr: &actionExpr{ + pos: position{line: 742, col: 13, offset: 14563}, + run: (*parser).callonStringIdentifier6, + expr: &choiceExpr{ + pos: position{line: 742, col: 15, offset: 14565}, + alternatives: []interface{}{ + &charClassMatcher{ + pos: position{line: 742, col: 15, offset: 14565}, + val: "[^\"\\r\\n\\\\]", + chars: []rune{'"', '\r', '\n', '\\'}, + ignoreCase: false, + inverted: true, + }, + &seqExpr{ + pos: position{line: 742, col: 28, offset: 14578}, + exprs: []interface{}{ + &litMatcher{ + pos: position{line: 742, col: 28, offset: 14578}, + val: "\\", + ignoreCase: false, + }, + &anyMatcher{ + line: 742, col: 33, offset: 14583, + }, + }, + }, + }, + }, + }, + }, + }, + &litMatcher{ + pos: position{line: 742, col: 63, offset: 14613}, + val: "\"", + ignoreCase: false, + }, + }, + }, + }, + }, + { + name: "_", + pos: position{line: 748, col: 1, offset: 14691}, + expr: &zeroOrMoreExpr{ + pos: position{line: 749, col: 4, offset: 14696}, + expr: &choiceExpr{ + pos: position{line: 749, col: 6, offset: 14698}, + alternatives: []interface{}{ + &ruleRefExpr{ + pos: position{line: 749, col: 6, offset: 14698}, + name: "Whitespace", + }, + &ruleRefExpr{ + pos: position{line: 749, col: 19, offset: 14711}, + name: "Newline", + }, + }, + }, + }, + }, + { + name: "Newline", + pos: position{line: 751, col: 1, offset: 14723}, + expr: &choiceExpr{ + pos: position{line: 752, col: 4, offset: 14734}, + alternatives: []interface{}{ + &litMatcher{ + pos: position{line: 752, col: 4, offset: 14734}, + val: "\r\n", + ignoreCase: false, + }, + &litMatcher{ + pos: position{line: 753, col: 4, offset: 14744}, + val: "\r", + ignoreCase: false, + }, + &litMatcher{ + pos: position{line: 754, col: 4, offset: 14752}, + val: "\n", + ignoreCase: false, + }, + }, + }, + }, + { + name: "Whitespace", + pos: position{line: 756, col: 1, offset: 14758}, + expr: &choiceExpr{ + pos: position{line: 757, col: 4, offset: 14772}, + alternatives: []interface{}{ + &litMatcher{ + pos: position{line: 757, col: 4, offset: 14772}, + val: " ", + ignoreCase: false, + }, + &litMatcher{ + pos: position{line: 758, col: 4, offset: 14779}, + val: "\t", + ignoreCase: false, + }, + &litMatcher{ + pos: position{line: 759, col: 4, offset: 14787}, + val: "\v", + ignoreCase: false, + }, + &litMatcher{ + pos: position{line: 760, col: 4, offset: 14795}, + val: "\f", + ignoreCase: false, + }, + }, + }, + }, + { + name: "EOF", + pos: position{line: 762, col: 1, offset: 14801}, + expr: ¬Expr{ + pos: position{line: 763, col: 4, offset: 14808}, + expr: &anyMatcher{ + line: 763, col: 5, offset: 14809, + }, + }, + }, + }, +} + +func (c *current) onS10(s interface{}) (interface{}, error) { + return s, nil +} + +func (p *parser) callonS10() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onS10(stack["s"]) +} + +func (c *current) onS1(x, xs interface{}) (interface{}, error) { + return prepend(x, xs), nil +} + +func (p *parser) callonS1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onS1(stack["x"], stack["xs"]) +} + +func (c *current) onSelectStmt9(s interface{}) (interface{}, error) { + return s, nil +} + +func (p *parser) callonSelectStmt9() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onSelectStmt9(stack["s"]) +} + +func (c *current) onSelectStmt18(i interface{}) (interface{}, error) { + return i, nil +} + +func (p *parser) callonSelectStmt18() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onSelectStmt18(stack["i"]) +} + +func (c *current) onSelectStmt27(w interface{}) (interface{}, error) { + return w, nil +} + +func (p *parser) callonSelectStmt27() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onSelectStmt27(stack["w"]) +} + +func (c *current) onSelectStmt34(g interface{}) (interface{}, error) { + return g, nil +} + +func (p *parser) callonSelectStmt34() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onSelectStmt34(stack["g"]) +} + +func (c *current) onSelectStmt41(or interface{}) (interface{}, error) { + return or, nil +} + +func (p *parser) callonSelectStmt41() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onSelectStmt41(stack["or"]) +} + +func (c *current) onSelectStmt48(l interface{}) (interface{}, error) { + return l, nil +} + +func (p *parser) callonSelectStmt48() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onSelectStmt48(stack["l"]) +} + +func (c *current) onSelectStmt55(of interface{}) (interface{}, error) { + return of, nil +} + +func (p *parser) callonSelectStmt55() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onSelectStmt55(stack["of"]) +} + +func (c *current) onSelectStmt1(f, fs, table, where, group, order, limit, offset interface{}) (interface{}, error) { + var ( + tableNode *identifierNode + whereNode *whereOptionNode + limitNode *limitOptionNode + offsetNode *offsetOptionNode + ) + if table != nil { + t := table.(identifierNode) + tableNode = &t + } + if where != nil { + w := where.(whereOptionNode) + whereNode = &w + } + if limit != nil { + l := limit.(limitOptionNode) + limitNode = &l + } + if offset != nil { + of := offset.(offsetOptionNode) + offsetNode = &of + } + return selectStmtNode{ + column: prepend(f, fs), + table: tableNode, + where: whereNode, + group: toSlice(group), + order: toSlice(order), + limit: limitNode, + offset: offsetNode, + }, nil +} + +func (p *parser) callonSelectStmt1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onSelectStmt1(stack["f"], stack["fs"], stack["table"], stack["where"], stack["group"], stack["order"], stack["limit"], stack["offset"]) +} + +func (c *current) onUpdateStmt14(s interface{}) (interface{}, error) { + return s, nil +} + +func (p *parser) callonUpdateStmt14() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onUpdateStmt14(stack["s"]) +} + +func (c *current) onUpdateStmt23(w interface{}) (interface{}, error) { + return w, nil +} + +func (p *parser) callonUpdateStmt23() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onUpdateStmt23(stack["w"]) +} + +func (c *current) onUpdateStmt1(table, a, as, where interface{}) (interface{}, error) { + var ( + whereNode *whereOptionNode + ) + if where != nil { + w := where.(whereOptionNode) + whereNode = &w + } + return updateStmtNode{ + table: table.(identifierNode), + assignment: prepend(a, as), + where: whereNode, + }, nil +} + +func (p *parser) callonUpdateStmt1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onUpdateStmt1(stack["table"], stack["a"], stack["as"], stack["where"]) +} + +func (c *current) onDeleteStmt11(w interface{}) (interface{}, error) { + return w, nil +} + +func (p *parser) callonDeleteStmt11() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onDeleteStmt11(stack["w"]) +} + +func (c *current) onDeleteStmt1(table, where interface{}) (interface{}, error) { + var ( + whereNode *whereOptionNode + ) + if where != nil { + w := where.(whereOptionNode) + whereNode = &w + } + return deleteStmtNode{ + table: table.(identifierNode), + where: whereNode, + }, nil +} + +func (p *parser) callonDeleteStmt1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onDeleteStmt1(stack["table"], stack["where"]) +} + +func (c *current) onInsertStmt1(table, insert interface{}) (interface{}, error) { + return insertStmtNode{ + table: table.(identifierNode), + insert: insert, + }, nil +} + +func (p *parser) callonInsertStmt1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onInsertStmt1(stack["table"], stack["insert"]) +} + +func (c *current) onInsertValue1(e interface{}) (interface{}, error) { + return e, nil +} + +func (p *parser) callonInsertValue1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onInsertValue1(stack["e"]) +} + +func (c *current) onCreateTableStmt20(t interface{}) (interface{}, error) { + return t, nil +} + +func (p *parser) callonCreateTableStmt20() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onCreateTableStmt20(stack["t"]) +} + +func (c *current) onCreateTableStmt14(s, ss interface{}) (interface{}, error) { + return prepend(s, ss), nil +} + +func (p *parser) callonCreateTableStmt14() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onCreateTableStmt14(stack["s"], stack["ss"]) +} + +func (c *current) onCreateTableStmt1(table, column interface{}) (interface{}, error) { + return createTableStmtNode{ + table: table.(identifierNode), + column: toSlice(column), + }, nil +} + +func (p *parser) callonCreateTableStmt1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onCreateTableStmt1(stack["table"], stack["column"]) +} + +func (c *current) onColumnSchema10(s interface{}) (interface{}, error) { + return s, nil +} + +func (p *parser) callonColumnSchema10() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onColumnSchema10(stack["s"]) +} + +func (c *current) onColumnSchema1(i, t, cs interface{}) (interface{}, error) { + return columnSchemaNode{ + column: i.(identifierNode), + dataType: t, + constraint: toSlice(cs), + }, nil +} + +func (p *parser) callonColumnSchema1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onColumnSchema1(stack["i"], stack["t"], stack["cs"]) +} + +func (c *current) onCreateIndexStmt6(u interface{}) (interface{}, error) { + return u, nil +} + +func (p *parser) callonCreateIndexStmt6() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onCreateIndexStmt6(stack["u"]) +} + +func (c *current) onCreateIndexStmt28(x interface{}) (interface{}, error) { + return x, nil +} + +func (p *parser) callonCreateIndexStmt28() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onCreateIndexStmt28(stack["x"]) +} + +func (c *current) onCreateIndexStmt1(unique, index, table, i, is interface{}) (interface{}, error) { + var ( + uniqueNode *uniqueOptionNode + ) + if unique != nil { + u := unique.(uniqueOptionNode) + uniqueNode = &u + } + return createIndexStmtNode{ + index: index.(identifierNode), + table: table.(identifierNode), + column: prepend(i, is), + unique: uniqueNode, + }, nil +} + +func (p *parser) callonCreateIndexStmt1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onCreateIndexStmt1(stack["unique"], stack["index"], stack["table"], stack["i"], stack["is"]) +} + +func (c *current) onWhereClause1(e interface{}) (interface{}, error) { + return whereOptionNode{condition: e}, nil +} + +func (p *parser) callonWhereClause1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onWhereClause1(stack["e"]) +} + +func (c *current) onOrderByClause11(s interface{}) (interface{}, error) { + return s, nil +} + +func (p *parser) callonOrderByClause11() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onOrderByClause11(stack["s"]) +} + +func (c *current) onOrderByClause1(f, fs interface{}) (interface{}, error) { + return prepend(f, fs), nil +} + +func (p *parser) callonOrderByClause1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onOrderByClause1(stack["f"], stack["fs"]) +} + +func (c *current) onOrderColumn7(t interface{}) (interface{}, error) { + return t, nil +} + +func (p *parser) callonOrderColumn7() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onOrderColumn7(stack["t"]) +} + +func (c *current) onOrderColumn16(l interface{}) (interface{}, error) { + return l, nil +} + +func (p *parser) callonOrderColumn16() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onOrderColumn16(stack["l"]) +} + +func (c *current) onOrderColumn1(i, s, n interface{}) (interface{}, error) { + return orderOptionNode{ + expr: i, + desc: s != nil && string(s.([]byte)) == "desc", + nullfirst: n != nil && string(n.([]byte)) == "first", + }, nil +} + +func (p *parser) callonOrderColumn1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onOrderColumn1(stack["i"], stack["s"], stack["n"]) +} + +func (c *current) onGroupByClause11(s interface{}) (interface{}, error) { + return groupOptionNode{expr: s}, nil +} + +func (p *parser) callonGroupByClause11() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onGroupByClause11(stack["s"]) +} + +func (c *current) onGroupByClause1(i, is interface{}) (interface{}, error) { + return prepend(groupOptionNode{expr: i}, is), nil +} + +func (p *parser) callonGroupByClause1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onGroupByClause1(stack["i"], stack["is"]) +} + +func (c *current) onOffsetClause1(i interface{}) (interface{}, error) { + return offsetOptionNode{value: i.(integerValueNode)}, nil +} + +func (p *parser) callonOffsetClause1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onOffsetClause1(stack["i"]) +} + +func (c *current) onLimitClause1(i interface{}) (interface{}, error) { + return limitOptionNode{value: i.(integerValueNode)}, nil +} + +func (p *parser) callonLimitClause1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onLimitClause1(stack["i"]) +} + +func (c *current) onInsertWithColumnClause13(x interface{}) (interface{}, error) { + return x, nil +} + +func (p *parser) callonInsertWithColumnClause13() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onInsertWithColumnClause13(stack["x"]) +} + +func (c *current) onInsertWithColumnClause5(f, fs interface{}) (interface{}, error) { + return prepend(f, fs), nil +} + +func (p *parser) callonInsertWithColumnClause5() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onInsertWithColumnClause5(stack["f"], stack["fs"]) +} + +func (c *current) onInsertWithColumnClause29(y interface{}) (interface{}, error) { + return y, nil +} + +func (p *parser) callonInsertWithColumnClause29() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onInsertWithColumnClause29(stack["y"]) +} + +func (c *current) onInsertWithColumnClause1(cs, v, vs interface{}) (interface{}, error) { + return insertWithColumnOptionNode{ + column: toSlice(cs), + value: prepend(v, vs), + }, nil +} + +func (p *parser) callonInsertWithColumnClause1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onInsertWithColumnClause1(stack["cs"], stack["v"], stack["vs"]) +} + +func (c *current) onInsertWithDefaultClause1() (interface{}, error) { + return insertWithDefaultOptionNode{}, nil +} + +func (p *parser) callonInsertWithDefaultClause1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onInsertWithDefaultClause1() +} + +func (c *current) onPrimaryKeyClause1() (interface{}, error) { + return primaryOptionNode{}, nil +} + +func (p *parser) callonPrimaryKeyClause1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onPrimaryKeyClause1() +} + +func (c *current) onNotNullClause1() (interface{}, error) { + return notNullOptionNode{}, nil +} + +func (p *parser) callonNotNullClause1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onNotNullClause1() +} + +func (c *current) onUniqueClause1() (interface{}, error) { + return uniqueOptionNode{}, nil +} + +func (p *parser) callonUniqueClause1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onUniqueClause1() +} + +func (c *current) onDefaultClause1(e interface{}) (interface{}, error) { + return defaultOptionNode{value: e}, nil +} + +func (p *parser) callonDefaultClause1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onDefaultClause1(stack["e"]) +} + +func (c *current) onForeignClause1(t, f interface{}) (interface{}, error) { + return foreignOptionNode{ + table: t.(identifierNode), + column: f.(identifierNode), + }, nil +} + +func (p *parser) callonForeignClause1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onForeignClause1(stack["t"], stack["f"]) +} + +func (c *current) onAutoincrementClause1() (interface{}, error) { + return autoincrementOptionNode{}, nil +} + +func (p *parser) callonAutoincrementClause1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onAutoincrementClause1() +} + +func (c *current) onExprWithDefault2(d interface{}) (interface{}, error) { + return d, nil +} + +func (p *parser) callonExprWithDefault2() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onExprWithDefault2(stack["d"]) +} + +func (c *current) onLogicExpr47(op, s interface{}) (interface{}, error) { + return opSetSubject(op, s), nil +} + +func (p *parser) callonLogicExpr47() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onLogicExpr47(stack["op"], stack["s"]) +} + +func (c *current) onLogicExpr41(o, os interface{}) (interface{}, error) { + return rightJoinOperators(o, os), nil +} + +func (p *parser) callonLogicExpr41() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onLogicExpr41(stack["o"], stack["os"]) +} + +func (c *current) onLogicExpr37(op, s interface{}) (interface{}, error) { + return opSetSubject(op, s), nil +} + +func (p *parser) callonLogicExpr37() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onLogicExpr37(stack["op"], stack["s"]) +} + +func (c *current) onLogicExpr31(o, os interface{}) (interface{}, error) { + return rightJoinOperators(o, os), nil +} + +func (p *parser) callonLogicExpr31() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onLogicExpr31(stack["o"], stack["os"]) +} + +func (c *current) onLogicExpr22(op, s interface{}) (interface{}, error) { + return opSetTarget(op, s), nil +} + +func (p *parser) callonLogicExpr22() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onLogicExpr22(stack["op"], stack["s"]) +} + +func (c *current) onLogicExpr17(l interface{}) (interface{}, error) { + return l, nil +} + +func (p *parser) callonLogicExpr17() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onLogicExpr17(stack["l"]) +} + +func (c *current) onLogicExpr11(o, os interface{}) (interface{}, error) { + return rightJoinOperators(o, os), nil +} + +func (p *parser) callonLogicExpr11() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onLogicExpr11(stack["o"], stack["os"]) +} + +func (c *current) onLogicExpr1In5(t interface{}) (interface{}, error) { + return t, nil +} + +func (p *parser) callonLogicExpr1In5() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onLogicExpr1In5(stack["t"]) +} + +func (c *current) onLogicExpr1In1(n, s interface{}) (interface{}, error) { + op := opSetSubject(&inOperatorNode{}, s) + if n != nil { + return opSetTarget(n, op), nil + } + return op, nil +} + +func (p *parser) callonLogicExpr1In1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onLogicExpr1In1(stack["n"], stack["s"]) +} + +func (c *current) onLogicExpr1Null6(t interface{}) (interface{}, error) { + return t, nil +} + +func (p *parser) callonLogicExpr1Null6() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onLogicExpr1Null6(stack["t"]) +} + +func (c *current) onLogicExpr1Null1(n interface{}) (interface{}, error) { + op := opSetSubject(&isOperatorNode{}, nullValueNode{}) + if n != nil { + return opSetTarget(n, op), nil + } + return op, nil +} + +func (p *parser) callonLogicExpr1Null1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onLogicExpr1Null1(stack["n"]) +} + +func (c *current) onLogicExpr1Like5(t interface{}) (interface{}, error) { + return t, nil +} + +func (p *parser) callonLogicExpr1Like5() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onLogicExpr1Like5(stack["t"]) +} + +func (c *current) onLogicExpr1Like1(n, s interface{}) (interface{}, error) { + op := opSetSubject(&likeOperatorNode{}, s) + if n != nil { + return opSetTarget(n, op), nil + } + return op, nil +} + +func (p *parser) callonLogicExpr1Like1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onLogicExpr1Like1(stack["n"], stack["s"]) +} + +func (c *current) onLogicExpr1Cmp1(op, s interface{}) (interface{}, error) { + return opSetSubject(op, s), nil +} + +func (p *parser) callonLogicExpr1Cmp1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onLogicExpr1Cmp1(stack["op"], stack["s"]) +} + +func (c *current) onArithmeticExpr37(op, s interface{}) (interface{}, error) { + return opSetSubject(op, s), nil +} + +func (p *parser) callonArithmeticExpr37() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onArithmeticExpr37(stack["op"], stack["s"]) +} + +func (c *current) onArithmeticExpr31(o, os interface{}) (interface{}, error) { + return rightJoinOperators(o, os), nil +} + +func (p *parser) callonArithmeticExpr31() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onArithmeticExpr31(stack["o"], stack["os"]) +} + +func (c *current) onArithmeticExpr27(op, s interface{}) (interface{}, error) { + return opSetSubject(op, s), nil +} + +func (p *parser) callonArithmeticExpr27() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onArithmeticExpr27(stack["op"], stack["s"]) +} + +func (c *current) onArithmeticExpr21(o, os interface{}) (interface{}, error) { + return rightJoinOperators(o, os), nil +} + +func (p *parser) callonArithmeticExpr21() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onArithmeticExpr21(stack["o"], stack["os"]) +} + +func (c *current) onArithmeticExpr17(op, s interface{}) (interface{}, error) { + return opSetSubject(op, s), nil +} + +func (p *parser) callonArithmeticExpr17() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onArithmeticExpr17(stack["op"], stack["s"]) +} + +func (c *current) onArithmeticExpr11(o, os interface{}) (interface{}, error) { + return rightJoinOperators(o, os), nil +} + +func (p *parser) callonArithmeticExpr11() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onArithmeticExpr11(stack["o"], stack["os"]) +} + +func (c *current) onMultiExpr7(e interface{}) (interface{}, error) { + return e, nil +} + +func (p *parser) callonMultiExpr7() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onMultiExpr7(stack["e"]) +} + +func (c *current) onMultiExpr1(x, xs interface{}) (interface{}, error) { + return prepend(x, xs), nil +} + +func (p *parser) callonMultiExpr1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onMultiExpr1(stack["x"], stack["xs"]) +} + +func (c *current) onMultiExprWithDefault7(e interface{}) (interface{}, error) { + return e, nil +} + +func (p *parser) callonMultiExprWithDefault7() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onMultiExprWithDefault7(stack["e"]) +} + +func (c *current) onMultiExprWithDefault1(x, xs interface{}) (interface{}, error) { + return prepend(x, xs), nil +} + +func (p *parser) callonMultiExprWithDefault1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onMultiExprWithDefault1(stack["x"], stack["xs"]) +} + +func (c *current) onOperand2(op, s interface{}) (interface{}, error) { + return opSetTarget(op, s), nil +} + +func (p *parser) callonOperand2() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onOperand2(stack["op"], stack["s"]) +} + +func (c *current) onOperand9(e interface{}) (interface{}, error) { + return e, nil +} + +func (p *parser) callonOperand9() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onOperand9(stack["e"]) +} + +func (c *current) onOperand17(t interface{}) (interface{}, error) { + return t, nil +} + +func (p *parser) callonOperand17() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onOperand17(stack["t"]) +} + +func (c *current) onTypeCast1(o, s interface{}) (interface{}, error) { + return opSetSubject(opSetObject(&castOperatorNode{}, o), s), nil +} + +func (p *parser) callonTypeCast1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onTypeCast1(stack["o"], stack["s"]) +} + +func (c *current) onFunctionCall1(i, r interface{}) (interface{}, error) { + return opSetSubject(opSetObject(&functionOperatorNode{}, i), r), nil +} + +func (p *parser) callonFunctionCall1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onFunctionCall1(stack["i"], stack["r"]) +} + +func (c *current) onFunctionArgs2(a interface{}) (interface{}, error) { + return []interface{}{a}, nil +} + +func (p *parser) callonFunctionArgs2() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onFunctionArgs2(stack["a"]) +} + +func (c *current) onAssignment1(i, e interface{}) (interface{}, error) { + return opSetSubject(opSetObject(&assignOperatorNode{}, i), e), nil +} + +func (p *parser) callonAssignment1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onAssignment1(stack["i"], stack["e"]) +} + +func (c *current) onSignOperator1() (interface{}, error) { + switch string(c.text) { + case "+": + return &posOperatorNode{}, nil + case "-": + return &negOperatorNode{}, nil + } + return nil, errors.New("unknown sign") +} + +func (p *parser) callonSignOperator1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onSignOperator1() +} + +func (c *current) onNotOperator1() (interface{}, error) { + return ¬OperatorNode{}, nil +} + +func (p *parser) callonNotOperator1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onNotOperator1() +} + +func (c *current) onAndOperator1() (interface{}, error) { + return &andOperatorNode{}, nil +} + +func (p *parser) callonAndOperator1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onAndOperator1() +} + +func (c *current) onOrOperator1() (interface{}, error) { + return &orOperatorNode{}, nil +} + +func (p *parser) callonOrOperator1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onOrOperator1() +} + +func (c *current) onCmpOperator1() (interface{}, error) { + switch string(c.text) { + case "<=": + return &lessOrEqualOperatorNode{}, nil + case ">=": + return &greaterOrEqualOperatorNode{}, nil + case "<>": + return ¬EqualOperatorNode{}, nil + case "!=": + return ¬EqualOperatorNode{}, nil + case "<": + return &lessOperatorNode{}, nil + case ">": + return &greaterOperatorNode{}, nil + case "=": + return &equalOperatorNode{}, nil + } + return nil, errors.New("unknown cmp") +} + +func (p *parser) callonCmpOperator1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onCmpOperator1() +} + +func (c *current) onConcatOperator1() (interface{}, error) { + return &concatOperatorNode{}, nil +} + +func (p *parser) callonConcatOperator1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onConcatOperator1() +} + +func (c *current) onAddSubOperator1() (interface{}, error) { + switch string(c.text) { + case "+": + return &addOperatorNode{}, nil + case "-": + return &subOperatorNode{}, nil + } + return nil, errors.New("unknown add sub") +} + +func (p *parser) callonAddSubOperator1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onAddSubOperator1() +} + +func (c *current) onMulDivModOperator1() (interface{}, error) { + switch string(c.text) { + case "*": + return &mulOperatorNode{}, nil + case "/": + return &divOperatorNode{}, nil + case "%": + return &modOperatorNode{}, nil + } + return nil, errors.New("unknown mul div mod") +} + +func (p *parser) callonMulDivModOperator1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onMulDivModOperator1() +} + +func (c *current) onUIntType1(s interface{}) (interface{}, error) { + return intTypeNode{size: toInt(s.([]byte)), unsigned: true}, nil +} + +func (p *parser) callonUIntType1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onUIntType1(stack["s"]) +} + +func (c *current) onIntType1(s interface{}) (interface{}, error) { + return intTypeNode{size: toInt(s.([]byte))}, nil +} + +func (p *parser) callonIntType1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onIntType1(stack["s"]) +} + +func (c *current) onUFixedType1(s, t interface{}) (interface{}, error) { + return fixedTypeNode{ + integerSize: toInt(s.([]byte)), + fractionalSize: toInt(t.([]byte)), + unsigned: true, + }, nil +} + +func (p *parser) callonUFixedType1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onUFixedType1(stack["s"], stack["t"]) +} + +func (c *current) onFixedType1(s, t interface{}) (interface{}, error) { + return fixedTypeNode{ + integerSize: toInt(s.([]byte)), + fractionalSize: toInt(t.([]byte)), + }, nil +} + +func (p *parser) callonFixedType1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onFixedType1(stack["s"], stack["t"]) +} + +func (c *current) onFixedBytesType2(s interface{}) (interface{}, error) { + return fixedBytesTypeNode{size: toInt(s.([]byte))}, nil +} + +func (p *parser) callonFixedBytesType2() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onFixedBytesType2(stack["s"]) +} + +func (c *current) onFixedBytesType9() (interface{}, error) { + return fixedBytesTypeNode{size: 1}, nil +} + +func (p *parser) callonFixedBytesType9() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onFixedBytesType9() +} + +func (c *current) onDynamicBytesType1() (interface{}, error) { + return dynamicBytesTypeNode{}, nil +} + +func (p *parser) callonDynamicBytesType1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onDynamicBytesType1() +} + +func (c *current) onAddressType1() (interface{}, error) { + return addressTypeNode{}, nil +} + +func (p *parser) callonAddressType1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onAddressType1() +} + +func (c *current) onBoolType1() (interface{}, error) { + return boolTypeNode{}, nil +} + +func (p *parser) callonBoolType1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onBoolType1() +} + +func (c *current) onAnyLiteral1() (interface{}, error) { + return anyValueNode{}, nil +} + +func (p *parser) callonAnyLiteral1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onAnyLiteral1() +} + +func (c *current) onDefaultLiteral1() (interface{}, error) { + return defaultValueNode{}, nil +} + +func (p *parser) callonDefaultLiteral1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onDefaultLiteral1() +} + +func (c *current) onBoolLiteral1(b interface{}) (interface{}, error) { + return boolValueNode(string(b.([]byte)) == "true"), nil +} + +func (p *parser) callonBoolLiteral1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onBoolLiteral1(stack["b"]) +} + +func (c *current) onNullLiteral1() (interface{}, error) { + return nullValueNode{}, nil +} + +func (p *parser) callonNullLiteral1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onNullLiteral1() +} + +func (c *current) onNumberLiteral2(h interface{}) (interface{}, error) { + return h, nil +} + +func (p *parser) callonNumberLiteral2() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onNumberLiteral2(stack["h"]) +} + +func (c *current) onInteger1() (interface{}, error) { + return integerValueNode{v: toDecimal(c.text)}, nil +} + +func (p *parser) callonInteger1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onInteger1() +} + +func (c *current) onNonZeroLeadingInteger1() (interface{}, error) { + return c.text, nil +} + +func (p *parser) callonNonZeroLeadingInteger1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onNonZeroLeadingInteger1() +} + +func (c *current) onDecimal1() (interface{}, error) { + return decimalValueNode(toDecimal(c.text)), nil +} + +func (p *parser) callonDecimal1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onDecimal1() +} + +func (c *current) onHex1(s interface{}) (interface{}, error) { + return hexToInteger(joinBytes(s)), nil +} + +func (p *parser) callonHex1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onHex1(stack["s"]) +} + +func (c *current) onHexString9() (interface{}, error) { + return c.text, nil +} + +func (p *parser) callonHexString9() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onHexString9() +} + +func (c *current) onHexString1(s interface{}) (interface{}, error) { + return bytesValueNode(hexToBytes(joinBytes(s))), nil +} + +func (p *parser) callonHexString1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onHexString1(stack["s"]) +} + +func (c *current) onNormalString6() (interface{}, error) { + return c.text, nil +} + +func (p *parser) callonNormalString6() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onNormalString6() +} + +func (c *current) onNormalString1(s interface{}) (interface{}, error) { + return bytesValueNode(resolveString(joinBytes(s))), nil +} + +func (p *parser) callonNormalString1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onNormalString1(stack["s"]) +} + +func (c *current) onTrueToken1() (interface{}, error) { + return toLower(c.text), nil +} + +func (p *parser) callonTrueToken1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onTrueToken1() +} + +func (c *current) onFalseToken1() (interface{}, error) { + return toLower(c.text), nil +} + +func (p *parser) callonFalseToken1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onFalseToken1() +} + +func (c *current) onLastToken1() (interface{}, error) { + return toLower(c.text), nil +} + +func (p *parser) callonLastToken1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onLastToken1() +} + +func (c *current) onFirstToken1() (interface{}, error) { + return toLower(c.text), nil +} + +func (p *parser) callonFirstToken1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onFirstToken1() +} + +func (c *current) onAscToken1() (interface{}, error) { + return toLower(c.text), nil +} + +func (p *parser) callonAscToken1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onAscToken1() +} + +func (c *current) onDescToken1() (interface{}, error) { + return toLower(c.text), nil +} + +func (p *parser) callonDescToken1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onDescToken1() +} + +func (c *current) onNormalIdentifier1() (interface{}, error) { + return identifierNode(c.text), nil +} + +func (p *parser) callonNormalIdentifier1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onNormalIdentifier1() +} + +func (c *current) onStringIdentifier6() (interface{}, error) { + return c.text, nil +} + +func (p *parser) callonStringIdentifier6() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onStringIdentifier6() +} + +func (c *current) onStringIdentifier1(s interface{}) (interface{}, error) { + return identifierNode(resolveString(joinBytes(s))), nil +} + +func (p *parser) callonStringIdentifier1() (interface{}, error) { + stack := p.vstack[len(p.vstack)-1] + _ = stack + return p.cur.onStringIdentifier1(stack["s"]) +} + +var ( + // errNoRule is returned when the grammar to parse has no rule. + errNoRule = errors.New("grammar has no rule") + + // errInvalidEntrypoint is returned when the specified entrypoint rule + // does not exit. + errInvalidEntrypoint = errors.New("invalid entrypoint") + + // errInvalidEncoding is returned when the source is not properly + // utf8-encoded. + errInvalidEncoding = errors.New("invalid encoding") + + // errMaxExprCnt is used to signal that the maximum number of + // expressions have been parsed. + errMaxExprCnt = errors.New("max number of expresssions parsed") +) + +// Option is a function that can set an option on the parser. It returns +// the previous setting as an Option. +type Option func(*parser) Option + +// MaxExpressions creates an Option to stop parsing after the provided +// number of expressions have been parsed, if the value is 0 then the parser will +// parse for as many steps as needed (possibly an infinite number). +// +// The default for maxExprCnt is 0. +func MaxExpressions(maxExprCnt uint64) Option { + return func(p *parser) Option { + oldMaxExprCnt := p.maxExprCnt + p.maxExprCnt = maxExprCnt + return MaxExpressions(oldMaxExprCnt) + } +} + +// Entrypoint creates an Option to set the rule name to use as entrypoint. +// The rule name must have been specified in the -alternate-entrypoints +// if generating the parser with the -optimize-grammar flag, otherwise +// it may have been optimized out. Passing an empty string sets the +// entrypoint to the first rule in the grammar. +// +// The default is to start parsing at the first rule in the grammar. +func Entrypoint(ruleName string) Option { + return func(p *parser) Option { + oldEntrypoint := p.entrypoint + p.entrypoint = ruleName + if ruleName == "" { + p.entrypoint = g.rules[0].name + } + return Entrypoint(oldEntrypoint) + } +} + +// Statistics adds a user provided Stats struct to the parser to allow +// the user to process the results after the parsing has finished. +// Also the key for the "no match" counter is set. +// +// Example usage: +// +// input := "input" +// stats := Stats{} +// _, err := Parse("input-file", []byte(input), Statistics(&stats, "no match")) +// if err != nil { +// log.Panicln(err) +// } +// b, err := json.MarshalIndent(stats.ChoiceAltCnt, "", " ") +// if err != nil { +// log.Panicln(err) +// } +// fmt.Println(string(b)) +// +func Statistics(stats *Stats, choiceNoMatch string) Option { + return func(p *parser) Option { + oldStats := p.Stats + p.Stats = stats + oldChoiceNoMatch := p.choiceNoMatch + p.choiceNoMatch = choiceNoMatch + if p.Stats.ChoiceAltCnt == nil { + p.Stats.ChoiceAltCnt = make(map[string]map[string]int) + } + return Statistics(oldStats, oldChoiceNoMatch) + } +} + +// Debug creates an Option to set the debug flag to b. When set to true, +// debugging information is printed to stdout while parsing. +// +// The default is false. +func Debug(b bool) Option { + return func(p *parser) Option { + old := p.debug + p.debug = b + return Debug(old) + } +} + +// Memoize creates an Option to set the memoize flag to b. When set to true, +// the parser will cache all results so each expression is evaluated only +// once. This guarantees linear parsing time even for pathological cases, +// at the expense of more memory and slower times for typical cases. +// +// The default is false. +func Memoize(b bool) Option { + return func(p *parser) Option { + old := p.memoize + p.memoize = b + return Memoize(old) + } +} + +// AllowInvalidUTF8 creates an Option to allow invalid UTF-8 bytes. +// Every invalid UTF-8 byte is treated as a utf8.RuneError (U+FFFD) +// by character class matchers and is matched by the any matcher. +// The returned matched value, c.text and c.offset are NOT affected. +// +// The default is false. +func AllowInvalidUTF8(b bool) Option { + return func(p *parser) Option { + old := p.allowInvalidUTF8 + p.allowInvalidUTF8 = b + return AllowInvalidUTF8(old) + } +} + +// Recover creates an Option to set the recover flag to b. When set to +// true, this causes the parser to recover from panics and convert it +// to an error. Setting it to false can be useful while debugging to +// access the full stack trace. +// +// The default is true. +func Recover(b bool) Option { + return func(p *parser) Option { + old := p.recover + p.recover = b + return Recover(old) + } +} + +// GlobalStore creates an Option to set a key to a certain value in +// the globalStore. +func GlobalStore(key string, value interface{}) Option { + return func(p *parser) Option { + old := p.cur.globalStore[key] + p.cur.globalStore[key] = value + return GlobalStore(key, old) + } +} + +// InitState creates an Option to set a key to a certain value in +// the global "state" store. +func InitState(key string, value interface{}) Option { + return func(p *parser) Option { + old := p.cur.state[key] + p.cur.state[key] = value + return InitState(key, old) + } +} + +// ParseFile parses the file identified by filename. +func ParseFile(filename string, opts ...Option) (i interface{}, err error) { + f, err := os.Open(filename) + if err != nil { + return nil, err + } + defer func() { + if closeErr := f.Close(); closeErr != nil { + err = closeErr + } + }() + return ParseReader(filename, f, opts...) +} + +// ParseReader parses the data from r using filename as information in the +// error messages. +func ParseReader(filename string, r io.Reader, opts ...Option) (interface{}, error) { + b, err := ioutil.ReadAll(r) + if err != nil { + return nil, err + } + + return Parse(filename, b, opts...) +} + +// Parse parses the data from b using filename as information in the +// error messages. +func Parse(filename string, b []byte, opts ...Option) (interface{}, error) { + return newParser(filename, b, opts...).parse(g) +} + +// position records a position in the text. +type position struct { + line, col, offset int +} + +func (p position) String() string { + return fmt.Sprintf("%d:%d [%d]", p.line, p.col, p.offset) +} + +// savepoint stores all state required to go back to this point in the +// parser. +type savepoint struct { + position + rn rune + w int +} + +type current struct { + pos position // start position of the match + text []byte // raw text of the match + + // state is a store for arbitrary key,value pairs that the user wants to be + // tied to the backtracking of the parser. + // This is always rolled back if a parsing rule fails. + state storeDict + + // globalStore is a general store for the user to store arbitrary key-value + // pairs that they need to manage and that they do not want tied to the + // backtracking of the parser. This is only modified by the user and never + // rolled back by the parser. It is always up to the user to keep this in a + // consistent state. + globalStore storeDict +} + +type storeDict map[string]interface{} + +// the AST types... + +type grammar struct { + pos position + rules []*rule +} + +type rule struct { + pos position + name string + displayName string + expr interface{} +} + +type choiceExpr struct { + pos position + alternatives []interface{} +} + +type actionExpr struct { + pos position + expr interface{} + run func(*parser) (interface{}, error) +} + +type recoveryExpr struct { + pos position + expr interface{} + recoverExpr interface{} + failureLabel []string +} + +type seqExpr struct { + pos position + exprs []interface{} +} + +type throwExpr struct { + pos position + label string +} + +type labeledExpr struct { + pos position + label string + expr interface{} +} + +type expr struct { + pos position + expr interface{} +} + +type andExpr expr +type notExpr expr +type zeroOrOneExpr expr +type zeroOrMoreExpr expr +type oneOrMoreExpr expr + +type ruleRefExpr struct { + pos position + name string +} + +type stateCodeExpr struct { + pos position + run func(*parser) error +} + +type andCodeExpr struct { + pos position + run func(*parser) (bool, error) +} + +type notCodeExpr struct { + pos position + run func(*parser) (bool, error) +} + +type litMatcher struct { + pos position + val string + ignoreCase bool +} + +type charClassMatcher struct { + pos position + val string + basicLatinChars [128]bool + chars []rune + ranges []rune + classes []*unicode.RangeTable + ignoreCase bool + inverted bool +} + +type anyMatcher position + +// errList cumulates the errors found by the parser. +type errList []error + +func (e *errList) add(err error) { + *e = append(*e, err) +} + +func (e errList) err() error { + if len(e) == 0 { + return nil + } + e.dedupe() + return e +} + +func (e *errList) dedupe() { + var cleaned []error + set := make(map[string]bool) + for _, err := range *e { + if msg := err.Error(); !set[msg] { + set[msg] = true + cleaned = append(cleaned, err) + } + } + *e = cleaned +} + +func (e errList) Error() string { + switch len(e) { + case 0: + return "" + case 1: + return e[0].Error() + default: + var buf bytes.Buffer + + for i, err := range e { + if i > 0 { + buf.WriteRune('\n') + } + buf.WriteString(err.Error()) + } + return buf.String() + } +} + +// parserError wraps an error with a prefix indicating the rule in which +// the error occurred. The original error is stored in the Inner field. +type parserError struct { + Inner error + pos position + prefix string + expected []string +} + +// Error returns the error message. +func (p *parserError) Error() string { + return p.prefix + ": " + p.Inner.Error() +} + +// newParser creates a parser with the specified input source and options. +func newParser(filename string, b []byte, opts ...Option) *parser { + stats := Stats{ + ChoiceAltCnt: make(map[string]map[string]int), + } + + p := &parser{ + filename: filename, + errs: new(errList), + data: b, + pt: savepoint{position: position{line: 1}}, + recover: true, + cur: current{ + state: make(storeDict), + globalStore: make(storeDict), + }, + maxFailPos: position{col: 1, line: 1}, + maxFailExpected: make([]string, 0, 20), + Stats: &stats, + // start rule is rule [0] unless an alternate entrypoint is specified + entrypoint: g.rules[0].name, + } + p.setOptions(opts) + + if p.maxExprCnt == 0 { + p.maxExprCnt = math.MaxUint64 + } + + return p +} + +// setOptions applies the options to the parser. +func (p *parser) setOptions(opts []Option) { + for _, opt := range opts { + opt(p) + } +} + +type resultTuple struct { + v interface{} + b bool + end savepoint +} + +const choiceNoMatch = -1 + +// Stats stores some statistics, gathered during parsing +type Stats struct { + // ExprCnt counts the number of expressions processed during parsing + // This value is compared to the maximum number of expressions allowed + // (set by the MaxExpressions option). + ExprCnt uint64 + + // ChoiceAltCnt is used to count for each ordered choice expression, + // which alternative is used how may times. + // These numbers allow to optimize the order of the ordered choice expression + // to increase the performance of the parser + // + // The outer key of ChoiceAltCnt is composed of the name of the rule as well + // as the line and the column of the ordered choice. + // The inner key of ChoiceAltCnt is the number (one-based) of the matching alternative. + // For each alternative the number of matches are counted. If an ordered choice does not + // match, a special counter is incremented. The name of this counter is set with + // the parser option Statistics. + // For an alternative to be included in ChoiceAltCnt, it has to match at least once. + ChoiceAltCnt map[string]map[string]int +} + +type parser struct { + filename string + pt savepoint + cur current + + data []byte + errs *errList + + depth int + recover bool + debug bool + + memoize bool + // memoization table for the packrat algorithm: + // map[offset in source] map[expression or rule] {value, match} + memo map[int]map[interface{}]resultTuple + + // rules table, maps the rule identifier to the rule node + rules map[string]*rule + // variables stack, map of label to value + vstack []map[string]interface{} + // rule stack, allows identification of the current rule in errors + rstack []*rule + + // parse fail + maxFailPos position + maxFailExpected []string + maxFailInvertExpected bool + + // max number of expressions to be parsed + maxExprCnt uint64 + // entrypoint for the parser + entrypoint string + + allowInvalidUTF8 bool + + *Stats + + choiceNoMatch string + // recovery expression stack, keeps track of the currently available recovery expression, these are traversed in reverse + recoveryStack []map[string]interface{} +} + +// push a variable set on the vstack. +func (p *parser) pushV() { + if cap(p.vstack) == len(p.vstack) { + // create new empty slot in the stack + p.vstack = append(p.vstack, nil) + } else { + // slice to 1 more + p.vstack = p.vstack[:len(p.vstack)+1] + } + + // get the last args set + m := p.vstack[len(p.vstack)-1] + if m != nil && len(m) == 0 { + // empty map, all good + return + } + + m = make(map[string]interface{}) + p.vstack[len(p.vstack)-1] = m +} + +// pop a variable set from the vstack. +func (p *parser) popV() { + // if the map is not empty, clear it + m := p.vstack[len(p.vstack)-1] + if len(m) > 0 { + // GC that map + p.vstack[len(p.vstack)-1] = nil + } + p.vstack = p.vstack[:len(p.vstack)-1] +} + +// push a recovery expression with its labels to the recoveryStack +func (p *parser) pushRecovery(labels []string, expr interface{}) { + if cap(p.recoveryStack) == len(p.recoveryStack) { + // create new empty slot in the stack + p.recoveryStack = append(p.recoveryStack, nil) + } else { + // slice to 1 more + p.recoveryStack = p.recoveryStack[:len(p.recoveryStack)+1] + } + + m := make(map[string]interface{}, len(labels)) + for _, fl := range labels { + m[fl] = expr + } + p.recoveryStack[len(p.recoveryStack)-1] = m +} + +// pop a recovery expression from the recoveryStack +func (p *parser) popRecovery() { + // GC that map + p.recoveryStack[len(p.recoveryStack)-1] = nil + + p.recoveryStack = p.recoveryStack[:len(p.recoveryStack)-1] +} + +func (p *parser) print(prefix, s string) string { + if !p.debug { + return s + } + + fmt.Printf("%s %d:%d:%d: %s [%#U]\n", + prefix, p.pt.line, p.pt.col, p.pt.offset, s, p.pt.rn) + return s +} + +func (p *parser) in(s string) string { + p.depth++ + return p.print(strings.Repeat(" ", p.depth)+">", s) +} + +func (p *parser) out(s string) string { + p.depth-- + return p.print(strings.Repeat(" ", p.depth)+"<", s) +} + +func (p *parser) addErr(err error) { + p.addErrAt(err, p.pt.position, []string{}) +} + +func (p *parser) addErrAt(err error, pos position, expected []string) { + var buf bytes.Buffer + if p.filename != "" { + buf.WriteString(p.filename) + } + if buf.Len() > 0 { + buf.WriteString(":") + } + buf.WriteString(fmt.Sprintf("%d:%d (%d)", pos.line, pos.col, pos.offset)) + if len(p.rstack) > 0 { + if buf.Len() > 0 { + buf.WriteString(": ") + } + rule := p.rstack[len(p.rstack)-1] + if rule.displayName != "" { + buf.WriteString("rule " + rule.displayName) + } else { + buf.WriteString("rule " + rule.name) + } + } + pe := &parserError{Inner: err, pos: pos, prefix: buf.String(), expected: expected} + p.errs.add(pe) +} + +func (p *parser) failAt(fail bool, pos position, want string) { + // process fail if parsing fails and not inverted or parsing succeeds and invert is set + if fail == p.maxFailInvertExpected { + if pos.offset < p.maxFailPos.offset { + return + } + + if pos.offset > p.maxFailPos.offset { + p.maxFailPos = pos + p.maxFailExpected = p.maxFailExpected[:0] + } + + if p.maxFailInvertExpected { + want = "!" + want + } + p.maxFailExpected = append(p.maxFailExpected, want) + } +} + +// read advances the parser to the next rune. +func (p *parser) read() { + p.pt.offset += p.pt.w + rn, n := utf8.DecodeRune(p.data[p.pt.offset:]) + p.pt.rn = rn + p.pt.w = n + p.pt.col++ + if rn == '\n' { + p.pt.line++ + p.pt.col = 0 + } + + if rn == utf8.RuneError && n == 1 { // see utf8.DecodeRune + if !p.allowInvalidUTF8 { + p.addErr(errInvalidEncoding) + } + } +} + +// restore parser position to the savepoint pt. +func (p *parser) restore(pt savepoint) { + if p.debug { + defer p.out(p.in("restore")) + } + if pt.offset == p.pt.offset { + return + } + p.pt = pt +} + +// Cloner is implemented by any value that has a Clone method, which returns a +// copy of the value. This is mainly used for types which are not passed by +// value (e.g map, slice, chan) or structs that contain such types. +// +// This is used in conjunction with the global state feature to create proper +// copies of the state to allow the parser to properly restore the state in +// the case of backtracking. +type Cloner interface { + Clone() interface{} +} + +// clone and return parser current state. +func (p *parser) cloneState() storeDict { + if p.debug { + defer p.out(p.in("cloneState")) + } + + state := make(storeDict, len(p.cur.state)) + for k, v := range p.cur.state { + if c, ok := v.(Cloner); ok { + state[k] = c.Clone() + } else { + state[k] = v + } + } + return state +} + +// restore parser current state to the state storeDict. +// every restoreState should applied only one time for every cloned state +func (p *parser) restoreState(state storeDict) { + if p.debug { + defer p.out(p.in("restoreState")) + } + p.cur.state = state +} + +// get the slice of bytes from the savepoint start to the current position. +func (p *parser) sliceFrom(start savepoint) []byte { + return p.data[start.position.offset:p.pt.position.offset] +} + +func (p *parser) getMemoized(node interface{}) (resultTuple, bool) { + if len(p.memo) == 0 { + return resultTuple{}, false + } + m := p.memo[p.pt.offset] + if len(m) == 0 { + return resultTuple{}, false + } + res, ok := m[node] + return res, ok +} + +func (p *parser) setMemoized(pt savepoint, node interface{}, tuple resultTuple) { + if p.memo == nil { + p.memo = make(map[int]map[interface{}]resultTuple) + } + m := p.memo[pt.offset] + if m == nil { + m = make(map[interface{}]resultTuple) + p.memo[pt.offset] = m + } + m[node] = tuple +} + +func (p *parser) buildRulesTable(g *grammar) { + p.rules = make(map[string]*rule, len(g.rules)) + for _, r := range g.rules { + p.rules[r.name] = r + } +} + +func (p *parser) parse(g *grammar) (val interface{}, err error) { + if len(g.rules) == 0 { + p.addErr(errNoRule) + return nil, p.errs.err() + } + + // TODO : not super critical but this could be generated + p.buildRulesTable(g) + + if p.recover { + // panic can be used in action code to stop parsing immediately + // and return the panic as an error. + defer func() { + if e := recover(); e != nil { + if p.debug { + defer p.out(p.in("panic handler")) + } + val = nil + switch e := e.(type) { + case error: + p.addErr(e) + default: + p.addErr(fmt.Errorf("%v", e)) + } + err = p.errs.err() + } + }() + } + + startRule, ok := p.rules[p.entrypoint] + if !ok { + p.addErr(errInvalidEntrypoint) + return nil, p.errs.err() + } + + p.read() // advance to first rune + val, ok = p.parseRule(startRule) + if !ok { + if len(*p.errs) == 0 { + // If parsing fails, but no errors have been recorded, the expected values + // for the farthest parser position are returned as error. + maxFailExpectedMap := make(map[string]struct{}, len(p.maxFailExpected)) + for _, v := range p.maxFailExpected { + maxFailExpectedMap[v] = struct{}{} + } + expected := make([]string, 0, len(maxFailExpectedMap)) + eof := false + if _, ok := maxFailExpectedMap["!."]; ok { + delete(maxFailExpectedMap, "!.") + eof = true + } + for k := range maxFailExpectedMap { + expected = append(expected, k) + } + sort.Strings(expected) + if eof { + expected = append(expected, "EOF") + } + p.addErrAt(errors.New("no match found, expected: "+listJoin(expected, ", ", "or")), p.maxFailPos, expected) + } + + return nil, p.errs.err() + } + return val, p.errs.err() +} + +func listJoin(list []string, sep string, lastSep string) string { + switch len(list) { + case 0: + return "" + case 1: + return list[0] + default: + return fmt.Sprintf("%s %s %s", strings.Join(list[:len(list)-1], sep), lastSep, list[len(list)-1]) + } +} + +func (p *parser) parseRule(rule *rule) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseRule " + rule.name)) + } + + if p.memoize { + res, ok := p.getMemoized(rule) + if ok { + p.restore(res.end) + return res.v, res.b + } + } + + start := p.pt + p.rstack = append(p.rstack, rule) + p.pushV() + val, ok := p.parseExpr(rule.expr) + p.popV() + p.rstack = p.rstack[:len(p.rstack)-1] + if ok && p.debug { + p.print(strings.Repeat(" ", p.depth)+"MATCH", string(p.sliceFrom(start))) + } + + if p.memoize { + p.setMemoized(start, rule, resultTuple{val, ok, p.pt}) + } + return val, ok +} + +func (p *parser) parseExpr(expr interface{}) (interface{}, bool) { + var pt savepoint + + if p.memoize { + res, ok := p.getMemoized(expr) + if ok { + p.restore(res.end) + return res.v, res.b + } + pt = p.pt + } + + p.ExprCnt++ + if p.ExprCnt > p.maxExprCnt { + panic(errMaxExprCnt) + } + + var val interface{} + var ok bool + switch expr := expr.(type) { + case *actionExpr: + val, ok = p.parseActionExpr(expr) + case *andCodeExpr: + val, ok = p.parseAndCodeExpr(expr) + case *andExpr: + val, ok = p.parseAndExpr(expr) + case *anyMatcher: + val, ok = p.parseAnyMatcher(expr) + case *charClassMatcher: + val, ok = p.parseCharClassMatcher(expr) + case *choiceExpr: + val, ok = p.parseChoiceExpr(expr) + case *labeledExpr: + val, ok = p.parseLabeledExpr(expr) + case *litMatcher: + val, ok = p.parseLitMatcher(expr) + case *notCodeExpr: + val, ok = p.parseNotCodeExpr(expr) + case *notExpr: + val, ok = p.parseNotExpr(expr) + case *oneOrMoreExpr: + val, ok = p.parseOneOrMoreExpr(expr) + case *recoveryExpr: + val, ok = p.parseRecoveryExpr(expr) + case *ruleRefExpr: + val, ok = p.parseRuleRefExpr(expr) + case *seqExpr: + val, ok = p.parseSeqExpr(expr) + case *stateCodeExpr: + val, ok = p.parseStateCodeExpr(expr) + case *throwExpr: + val, ok = p.parseThrowExpr(expr) + case *zeroOrMoreExpr: + val, ok = p.parseZeroOrMoreExpr(expr) + case *zeroOrOneExpr: + val, ok = p.parseZeroOrOneExpr(expr) + default: + panic(fmt.Sprintf("unknown expression type %T", expr)) + } + if p.memoize { + p.setMemoized(pt, expr, resultTuple{val, ok, p.pt}) + } + return val, ok +} + +func (p *parser) parseActionExpr(act *actionExpr) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseActionExpr")) + } + + start := p.pt + val, ok := p.parseExpr(act.expr) + if ok { + p.cur.pos = start.position + p.cur.text = p.sliceFrom(start) + state := p.cloneState() + actVal, err := act.run(p) + if err != nil { + p.addErrAt(err, start.position, []string{}) + } + p.restoreState(state) + + val = actVal + } + if ok && p.debug { + p.print(strings.Repeat(" ", p.depth)+"MATCH", string(p.sliceFrom(start))) + } + return val, ok +} + +func (p *parser) parseAndCodeExpr(and *andCodeExpr) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseAndCodeExpr")) + } + + state := p.cloneState() + + ok, err := and.run(p) + if err != nil { + p.addErr(err) + } + p.restoreState(state) + + return nil, ok +} + +func (p *parser) parseAndExpr(and *andExpr) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseAndExpr")) + } + + pt := p.pt + state := p.cloneState() + p.pushV() + _, ok := p.parseExpr(and.expr) + p.popV() + p.restoreState(state) + p.restore(pt) + + return nil, ok +} + +func (p *parser) parseAnyMatcher(any *anyMatcher) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseAnyMatcher")) + } + + if p.pt.rn == utf8.RuneError && p.pt.w == 0 { + // EOF - see utf8.DecodeRune + p.failAt(false, p.pt.position, ".") + return nil, false + } + start := p.pt + p.read() + p.failAt(true, start.position, ".") + return p.sliceFrom(start), true +} + +func (p *parser) parseCharClassMatcher(chr *charClassMatcher) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseCharClassMatcher")) + } + + cur := p.pt.rn + start := p.pt + + // can't match EOF + if cur == utf8.RuneError && p.pt.w == 0 { // see utf8.DecodeRune + p.failAt(false, start.position, chr.val) + return nil, false + } + + if chr.ignoreCase { + cur = unicode.ToLower(cur) + } + + // try to match in the list of available chars + for _, rn := range chr.chars { + if rn == cur { + if chr.inverted { + p.failAt(false, start.position, chr.val) + return nil, false + } + p.read() + p.failAt(true, start.position, chr.val) + return p.sliceFrom(start), true + } + } + + // try to match in the list of ranges + for i := 0; i < len(chr.ranges); i += 2 { + if cur >= chr.ranges[i] && cur <= chr.ranges[i+1] { + if chr.inverted { + p.failAt(false, start.position, chr.val) + return nil, false + } + p.read() + p.failAt(true, start.position, chr.val) + return p.sliceFrom(start), true + } + } + + // try to match in the list of Unicode classes + for _, cl := range chr.classes { + if unicode.Is(cl, cur) { + if chr.inverted { + p.failAt(false, start.position, chr.val) + return nil, false + } + p.read() + p.failAt(true, start.position, chr.val) + return p.sliceFrom(start), true + } + } + + if chr.inverted { + p.read() + p.failAt(true, start.position, chr.val) + return p.sliceFrom(start), true + } + p.failAt(false, start.position, chr.val) + return nil, false +} + +func (p *parser) incChoiceAltCnt(ch *choiceExpr, altI int) { + choiceIdent := fmt.Sprintf("%s %d:%d", p.rstack[len(p.rstack)-1].name, ch.pos.line, ch.pos.col) + m := p.ChoiceAltCnt[choiceIdent] + if m == nil { + m = make(map[string]int) + p.ChoiceAltCnt[choiceIdent] = m + } + // We increment altI by 1, so the keys do not start at 0 + alt := strconv.Itoa(altI + 1) + if altI == choiceNoMatch { + alt = p.choiceNoMatch + } + m[alt]++ +} + +func (p *parser) parseChoiceExpr(ch *choiceExpr) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseChoiceExpr")) + } + + for altI, alt := range ch.alternatives { + // dummy assignment to prevent compile error if optimized + _ = altI + + state := p.cloneState() + + p.pushV() + val, ok := p.parseExpr(alt) + p.popV() + if ok { + p.incChoiceAltCnt(ch, altI) + return val, ok + } + p.restoreState(state) + } + p.incChoiceAltCnt(ch, choiceNoMatch) + return nil, false +} + +func (p *parser) parseLabeledExpr(lab *labeledExpr) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseLabeledExpr")) + } + + p.pushV() + val, ok := p.parseExpr(lab.expr) + p.popV() + if ok && lab.label != "" { + m := p.vstack[len(p.vstack)-1] + m[lab.label] = val + } + return val, ok +} + +func (p *parser) parseLitMatcher(lit *litMatcher) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseLitMatcher")) + } + + ignoreCase := "" + if lit.ignoreCase { + ignoreCase = "i" + } + val := fmt.Sprintf("%q%s", lit.val, ignoreCase) + start := p.pt + for _, want := range lit.val { + cur := p.pt.rn + if lit.ignoreCase { + cur = unicode.ToLower(cur) + } + if cur != want { + p.failAt(false, start.position, val) + p.restore(start) + return nil, false + } + p.read() + } + p.failAt(true, start.position, val) + return p.sliceFrom(start), true +} + +func (p *parser) parseNotCodeExpr(not *notCodeExpr) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseNotCodeExpr")) + } + + state := p.cloneState() + + ok, err := not.run(p) + if err != nil { + p.addErr(err) + } + p.restoreState(state) + + return nil, !ok +} + +func (p *parser) parseNotExpr(not *notExpr) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseNotExpr")) + } + + pt := p.pt + state := p.cloneState() + p.pushV() + p.maxFailInvertExpected = !p.maxFailInvertExpected + _, ok := p.parseExpr(not.expr) + p.maxFailInvertExpected = !p.maxFailInvertExpected + p.popV() + p.restoreState(state) + p.restore(pt) + + return nil, !ok +} + +func (p *parser) parseOneOrMoreExpr(expr *oneOrMoreExpr) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseOneOrMoreExpr")) + } + + var vals []interface{} + + for { + p.pushV() + val, ok := p.parseExpr(expr.expr) + p.popV() + if !ok { + if len(vals) == 0 { + // did not match once, no match + return nil, false + } + return vals, true + } + vals = append(vals, val) + } +} + +func (p *parser) parseRecoveryExpr(recover *recoveryExpr) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseRecoveryExpr (" + strings.Join(recover.failureLabel, ",") + ")")) + } + + p.pushRecovery(recover.failureLabel, recover.recoverExpr) + val, ok := p.parseExpr(recover.expr) + p.popRecovery() + + return val, ok +} + +func (p *parser) parseRuleRefExpr(ref *ruleRefExpr) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseRuleRefExpr " + ref.name)) + } + + if ref.name == "" { + panic(fmt.Sprintf("%s: invalid rule: missing name", ref.pos)) + } + + rule := p.rules[ref.name] + if rule == nil { + p.addErr(fmt.Errorf("undefined rule: %s", ref.name)) + return nil, false + } + return p.parseRule(rule) +} + +func (p *parser) parseSeqExpr(seq *seqExpr) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseSeqExpr")) + } + + vals := make([]interface{}, 0, len(seq.exprs)) + + pt := p.pt + state := p.cloneState() + for _, expr := range seq.exprs { + val, ok := p.parseExpr(expr) + if !ok { + p.restoreState(state) + p.restore(pt) + return nil, false + } + vals = append(vals, val) + } + return vals, true +} + +func (p *parser) parseStateCodeExpr(state *stateCodeExpr) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseStateCodeExpr")) + } + + err := state.run(p) + if err != nil { + p.addErr(err) + } + return nil, true +} + +func (p *parser) parseThrowExpr(expr *throwExpr) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseThrowExpr")) + } + + for i := len(p.recoveryStack) - 1; i >= 0; i-- { + if recoverExpr, ok := p.recoveryStack[i][expr.label]; ok { + if val, ok := p.parseExpr(recoverExpr); ok { + return val, ok + } + } + } + + return nil, false +} + +func (p *parser) parseZeroOrMoreExpr(expr *zeroOrMoreExpr) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseZeroOrMoreExpr")) + } + + var vals []interface{} + + for { + p.pushV() + val, ok := p.parseExpr(expr.expr) + p.popV() + if !ok { + return vals, true + } + vals = append(vals, val) + } +} + +func (p *parser) parseZeroOrOneExpr(expr *zeroOrOneExpr) (interface{}, bool) { + if p.debug { + defer p.out(p.in("parseZeroOrOneExpr")) + } + + p.pushV() + val, _ := p.parseExpr(expr.expr) + p.popV() + // whether it matched or not, consider it a match + return val, true +} diff --git a/core/vm/sqlvm/grammar.peg b/core/vm/sqlvm/grammar.peg new file mode 100644 index 000000000..ac2f8928c --- /dev/null +++ b/core/vm/sqlvm/grammar.peg @@ -0,0 +1,763 @@ +{ +package sqlvm +} + +S + <- _ x:Stmt? _ xs:( ';' _ s:Stmt? _ { return s, nil } )* EOF +{ return prepend(x, xs), nil } + +/* statement */ +Stmt + = SelectStmt + / UpdateStmt + / DeleteStmt + / InsertStmt + / CreateTableStmt + / CreateIndexStmt + +SelectStmt + = SelectToken + _ f:SelectColumn fs:( _ SeparatorToken _ s:SelectColumn { return s, nil } )* + table:( _ FromToken _ i:Identifier { return i, nil } )? + where:( _ w:WhereClause { return w, nil } )? + group:( _ g:GroupByClause { return g, nil } )? + order:( _ or:OrderByClause { return or, nil } )? + limit:( _ l:LimitClause { return l, nil } )? + offset:( _ of:OffsetClause { return of, nil } )? +{ + var ( + tableNode *identifierNode + whereNode *whereOptionNode + limitNode *limitOptionNode + offsetNode *offsetOptionNode + ) + if table != nil { + t := table.(identifierNode) + tableNode = &t + } + if where != nil { + w := where.(whereOptionNode) + whereNode = &w + } + if limit != nil { + l := limit.(limitOptionNode) + limitNode = &l + } + if offset != nil { + of := offset.(offsetOptionNode) + offsetNode = &of + } + return selectStmtNode{ + column: prepend(f, fs), + table: tableNode, + where: whereNode, + group: toSlice(group), + order: toSlice(order), + limit: limitNode, + offset: offsetNode, + }, nil +} + +SelectColumn + = AnyLiteral + / Expr + +UpdateStmt + = UpdateToken + _ table:Identifier + _ SetToken + _ a:Assignment as:( _ SeparatorToken _ s:Assignment { return s, nil } )* + where:( _ w:WhereClause { return w, nil } )? +{ + var ( + whereNode *whereOptionNode + ) + if where != nil { + w := where.(whereOptionNode) + whereNode = &w + } + return updateStmtNode{ + table: table.(identifierNode), + assignment: prepend(a, as), + where: whereNode, + }, nil +} + +DeleteStmt + = DeleteToken + _ FromToken + _ table:Identifier + where:( _ w:WhereClause { return w, nil } )? +{ + var ( + whereNode *whereOptionNode + ) + if where != nil { + w := where.(whereOptionNode) + whereNode = &w + } + return deleteStmtNode{ + table: table.(identifierNode), + where: whereNode, + }, nil +} + +InsertStmt + = InsertToken + _ IntoToken + _ table:Identifier + _ insert:( InsertWithColumnClause / InsertWithDefaultClause ) +{ + return insertStmtNode{ + table: table.(identifierNode), + insert: insert, + }, nil +} + +InsertValue + = '(' _ e:MultiExprWithDefault _ ')' +{ return e, nil } + +CreateTableStmt + = CreateToken + _ TableToken + _ table:Identifier + _ '(' + _ column:( + s:ColumnSchema + ss:( _ SeparatorToken _ t:ColumnSchema { return t, nil } )* + { return prepend(s, ss), nil } + )? + _ ')' +{ + return createTableStmtNode{ + table: table.(identifierNode), + column: toSlice(column), + }, nil +} + +ColumnSchema + = i:Identifier + _ t:DataType + cs:( _ s:ColumnConstraint { return s, nil } )* +{ + return columnSchemaNode{ + column: i.(identifierNode), + dataType: t, + constraint: toSlice(cs), + }, nil +} + +ColumnConstraint + = PrimaryKeyClause + / NotNullClause + / UniqueClause + / DefaultClause + / ForeignClause + / AutoincrementClause + +CreateIndexStmt + = CreateToken + unique:( _ u:UniqueClause { return u, nil } )? + _ IndexToken + _ index:Identifier + _ OnToken + _ table:Identifier + _ '(' _ i:Identifier is:( _ SeparatorToken _ x:Identifier { return x, nil } )* _ ')' +{ + var ( + uniqueNode *uniqueOptionNode + ) + if unique != nil { + u := unique.(uniqueOptionNode) + uniqueNode = &u + } + return createIndexStmtNode{ + index: index.(identifierNode), + table: table.(identifierNode), + column: prepend(i, is), + unique: uniqueNode, + }, nil +} + +/* clause */ +WhereClause + = WhereToken _ e:Expr +{ return whereOptionNode{condition: e}, nil } + +OrderByClause + = OrderToken + _ ByToken + _ f:OrderColumn + fs:( _ SeparatorToken _ s:OrderColumn { return s, nil } )* +{ return prepend(f, fs), nil } + +OrderColumn + = i:Expr + s:( _ t:( AscToken / DescToken ) { return t, nil } )? + n:( _ NullsToken _ l:( LastToken / FirstToken ) { return l, nil } )? +{ + return orderOptionNode{ + expr: i, + desc: s != nil && string(s.([]byte)) == "desc", + nullfirst: n != nil && string(n.([]byte)) == "first", + }, nil +} + +GroupByClause + = GroupToken + _ ByToken + _ i:Expr + is:( _ SeparatorToken _ s:Expr { return groupOptionNode{expr: s}, nil } )* +{ return prepend(groupOptionNode{expr: i}, is), nil } + +OffsetClause + = OffsetToken _ i:Integer +{ return offsetOptionNode{value: i.(integerValueNode)}, nil } + +LimitClause + = LimitToken _ i:Integer +{ return limitOptionNode{value: i.(integerValueNode)}, nil } + +InsertWithColumnClause + = cs:( '(' + _ f:Identifier + fs:( _ SeparatorToken _ x:Identifier { return x, nil } )* + _ ')' + _ { return prepend(f, fs), nil } + )? + ValuesToken + _ v:InsertValue + vs:( _ SeparatorToken _ y:InsertValue { return y, nil } )* +{ + return insertWithColumnOptionNode{ + column: toSlice(cs), + value: prepend(v, vs), + }, nil +} + +InsertWithDefaultClause + = DefaultToken _ ValuesToken +{ return insertWithDefaultOptionNode{}, nil } + +PrimaryKeyClause + = PrimaryToken _ KeyToken +{ return primaryOptionNode{}, nil } + +NotNullClause + = NotToken _ NullToken +{ return notNullOptionNode{}, nil } + +UniqueClause + = UniqueToken +{ return uniqueOptionNode{}, nil } + +DefaultClause + = DefaultToken _ e:Expr +{ return defaultOptionNode{value: e}, nil } + +ForeignClause + = ReferencesToken _ t:Identifier _ '(' _ f:Identifier _ ')' +{ + return foreignOptionNode{ + table: t.(identifierNode), + column: f.(identifierNode), + }, nil +} + +AutoincrementClause + = AutoincrementToken +{ return autoincrementOptionNode{}, nil } + +/* expression */ +Expr + = LogicExpr + +ExprWithDefault + = &(DefaultLiteral) d:DefaultLiteral { return d, nil } + / Expr + +LogicExpr + = LogicExpr4 + +LogicExpr4 + = o:LogicExpr3 + os:( _ op:OrOperator _ s:LogicExpr3 { return opSetSubject(op, s), nil } )* +{ return rightJoinOperators(o, os), nil } + +LogicExpr3 + = o:LogicExpr2 + os:( _ op:AndOperator _ s:LogicExpr2 { return opSetSubject(op, s), nil } )* +{ return rightJoinOperators(o, os), nil } + +LogicExpr2 + = op:NotOperator _ s:LogicExpr2 + { return opSetTarget(op, s), nil } + / LogicExpr1 + +LogicExpr1 + = o:ArithmeticExpr os:( _ l:LogicExpr1Op { return l, nil } )* +{ return rightJoinOperators(o, os), nil } + +LogicExpr1Op + = LogicExpr1In + / LogicExpr1Null + / LogicExpr1Like + / LogicExpr1Cmp + +LogicExpr1In + = n:( t:NotOperator _ { return t, nil } )? InToken _ '(' _ s:MultiExpr _ ')' +{ + op := opSetSubject(&inOperatorNode{}, s) + if n != nil { + return opSetTarget(n, op), nil + } + return op, nil +} + +LogicExpr1Null + = IsToken n:( _ t:NotOperator { return t, nil } )? _ NullToken +{ + op := opSetSubject(&isOperatorNode{}, nullValueNode{}) + if n != nil { + return opSetTarget(n, op), nil + } + return op, nil +} + +LogicExpr1Like + = n:( t:NotOperator _ { return t, nil } )? LikeToken _ s:Expr +{ + op := opSetSubject(&likeOperatorNode{}, s) + if n != nil { + return opSetTarget(n, op), nil + } + return op, nil +} + +LogicExpr1Cmp + = op:CmpOperator _ s:ArithmeticExpr +{ return opSetSubject(op, s), nil } + +ArithmeticExpr + = ArithmeticExpr3 + +ArithmeticExpr3 + = o:ArithmeticExpr2 os:( _ op:ConcatOperator _ s:ArithmeticExpr2 { return opSetSubject(op, s), nil } )* +{ return rightJoinOperators(o, os), nil } + +ArithmeticExpr2 + = o:ArithmeticExpr1 os:( _ op:AddSubOperator _ s:ArithmeticExpr1 { return opSetSubject(op, s), nil } )* +{ return rightJoinOperators(o, os), nil } + +ArithmeticExpr1 + = o:Operand os:( _ op:MulDivModOperator _ s:Operand { return opSetSubject(op, s), nil } )* +{ return rightJoinOperators(o, os), nil } + +MultiExpr + = x:Expr xs:( _ SeparatorToken _ e:Expr { return e, nil } )* +{ return prepend(x, xs), nil } + +MultiExprWithDefault + = x:ExprWithDefault xs:( _ SeparatorToken _ e:ExprWithDefault { return e, nil } )* +{ return prepend(x, xs), nil } + +Operand + = op:UnaryOperator _ s:Operand { return opSetTarget(op, s), nil } + / '(' _ e:Expr _ ')' { return e, nil } + / &(CastToken) t:TypeCast { return t, nil } + / FunctionCall + / Value + / Identifier + +TypeCast + = CastToken _ '(' _ o:Expr _ AsToken _ s:DataType _ ')' +{ return opSetSubject(opSetObject(&castOperatorNode{}, o), s), nil } + +FunctionCall + = i:Identifier _ '(' _ r:FunctionArgs? _ ')' +{ return opSetSubject(opSetObject(&functionOperatorNode{}, i), r), nil } + +FunctionArgs + = a:AnyLiteral { return []interface{}{a}, nil } + / MultiExpr + +Assignment + = i:Identifier _ '=' _ e:ExprWithDefault +{ return opSetSubject(opSetObject(&assignOperatorNode{}, i), e), nil } + +/* operator */ +UnaryOperator + = SignOperator + +SignOperator + = Sign +{ + switch string(c.text) { + case "+": + return &posOperatorNode{}, nil + case "-": + return &negOperatorNode{}, nil + } + return nil, errors.New("unknown sign") +} + +NotOperator + = NotToken +{ return ¬OperatorNode{}, nil } + +AndOperator + = AndToken +{ return &andOperatorNode{}, nil } + +OrOperator + = OrToken +{ return &orOperatorNode{}, nil } + +CmpOperator + = ( "<=" / ">=" / "<>" / "!=" / [<>=] ) +{ + switch string(c.text) { + case "<=": + return &lessOrEqualOperatorNode{}, nil + case ">=": + return &greaterOrEqualOperatorNode{}, nil + case "<>": + return ¬EqualOperatorNode{}, nil + case "!=": + return ¬EqualOperatorNode{}, nil + case "<": + return &lessOperatorNode{}, nil + case ">": + return &greaterOperatorNode{}, nil + case "=": + return &equalOperatorNode{}, nil + } + return nil, errors.New("unknown cmp") +} + +ConcatOperator + = "||" +{ return &concatOperatorNode{}, nil } + +AddSubOperator + = [+-] +{ + switch string(c.text) { + case "+": + return &addOperatorNode{}, nil + case "-": + return &subOperatorNode{}, nil + } + return nil, errors.New("unknown add sub") +} + +MulDivModOperator + = [*/%] +{ + switch string(c.text) { + case "*": + return &mulOperatorNode{}, nil + case "/": + return &divOperatorNode{}, nil + case "%": + return &modOperatorNode{}, nil + } + return nil, errors.New("unknown mul div mod") +} + +/* type */ +DataType + = UIntType + / IntType + / UFixedType + / FixedType + / FixedBytesType + / DynamicBytesType + / BoolType + / AddressType + +UIntType + = "UINT"i s:NonZeroLeadingInteger !NormalIdentifierRest +{ return intTypeNode{size: toInt(s.([]byte)), unsigned: true}, nil } + +IntType + = "INT"i s:NonZeroLeadingInteger !NormalIdentifierRest +{ return intTypeNode{size: toInt(s.([]byte))}, nil } + +UFixedType + = "UFIXED"i s:NonZeroLeadingInteger "X"i t:NonZeroLeadingInteger !NormalIdentifierRest +{ + return fixedTypeNode{ + integerSize: toInt(s.([]byte)), + fractionalSize: toInt(t.([]byte)), + unsigned: true, + }, nil +} + +FixedType + = "FIXED"i s:NonZeroLeadingInteger "X"i t:NonZeroLeadingInteger !NormalIdentifierRest +{ + return fixedTypeNode{ + integerSize: toInt(s.([]byte)), + fractionalSize: toInt(t.([]byte)), + }, nil +} + +FixedBytesType + = "BYTES"i s:NonZeroLeadingInteger !NormalIdentifierRest +{ return fixedBytesTypeNode{size: toInt(s.([]byte))}, nil } + / "BYTE"i !NormalIdentifierRest +{ return fixedBytesTypeNode{size: 1}, nil } + +DynamicBytesType + = ( "BYTES"i !NormalIdentifierRest + / "STRING"i !NormalIdentifierRest + / "TEXT"i !NormalIdentifierRest + ) +{ return dynamicBytesTypeNode{}, nil } + +AddressType + = "ADDRESS"i !NormalIdentifierRest +{ return addressTypeNode{}, nil } + +BoolType + = ( "BOOL"i !NormalIdentifierRest + / "BOOLEAN"i !NormalIdentifierRest + ) +{ return boolTypeNode{}, nil } + +/* value */ +Value + = NumberLiteral + / StringLiteral + / BoolLiteral + / NullLiteral + +AnyLiteral + = AnyToken +{ return anyValueNode{}, nil } + +DefaultLiteral + = DefaultToken +{ return defaultValueNode{}, nil } + +BoolLiteral + = b:( TrueToken / FalseToken ) +{ return boolValueNode(string(b.([]byte)) == "true"), nil } + +NullLiteral + = NullToken +{ return nullValueNode{}, nil } + +NumberLiteral + = &("0" "X"i) h:Hex { return h, nil } + / Decimal + +Sign + = [-+] + +Integer + = [0-9]+ +{ return integerValueNode{v: toDecimal(c.text)}, nil } + +NonZeroLeadingInteger + = ( "0" / [1-9][0-9]* ) +{ return c.text, nil } + +Fixnum + = Integer "." Integer + / Integer "."? + / "." Integer + +Decimal + = Fixnum ( "E"i Sign? Integer )? +{ return decimalValueNode(toDecimal(c.text)), nil } + +Hex + = "0" "X"i s:( [0-9A-Fa-f] )+ !NormalIdentifierRest +{ return hexToInteger(joinBytes(s)), nil } + +StringLiteral + = HexString + / NormalString + +HexString + = ( "HEX"i / "X"i ) "'" s:([0-9a-fA-F][0-9a-fA-F] { return c.text, nil } )* "'" +{ return bytesValueNode(hexToBytes(joinBytes(s))), nil } + +NormalString + = "'" s:( ( [^'\r\n\\] / "\\" . ) { return c.text, nil } )* "'" +{ return bytesValueNode(resolveString(joinBytes(s))), nil } + +/* token */ +SelectToken + = "SELECT"i !NormalIdentifierRest + +FromToken + = "FROM"i !NormalIdentifierRest + +WhereToken + = "WHERE"i !NormalIdentifierRest + +OrderToken + = "ORDER"i !NormalIdentifierRest + +ByToken + = "BY"i !NormalIdentifierRest + +GroupToken + = "GROUP"i !NormalIdentifierRest + +LimitToken + = "LIMIT"i !NormalIdentifierRest + +OffsetToken + = "OFFSET"i !NormalIdentifierRest + +UpdateToken + = "UPDATE"i !NormalIdentifierRest + +SetToken + = "SET"i !NormalIdentifierRest + +DeleteToken + = "DELETE"i !NormalIdentifierRest + +InsertToken + = "INSERT"i !NormalIdentifierRest + +IntoToken + = "INTO"i !NormalIdentifierRest + +ValuesToken + = "VALUES"i !NormalIdentifierRest + +CreateToken + = "CREATE"i !NormalIdentifierRest + +TableToken + = "TABLE"i !NormalIdentifierRest + +IndexToken + = "INDEX"i !NormalIdentifierRest + +UniqueToken + = "UNIQUE"i !NormalIdentifierRest + +DefaultToken + = "DEFAULT"i !NormalIdentifierRest + +PrimaryToken + = "PRIMARY"i !NormalIdentifierRest + +KeyToken + = "KEY"i !NormalIdentifierRest + +ReferencesToken + = "REFERENCES"i !NormalIdentifierRest + +AutoincrementToken + = "AUTOINCREMENT"i !NormalIdentifierRest + +OnToken + = "ON"i !NormalIdentifierRest + +TrueToken + = "TRUE"i !NormalIdentifierRest +{ return toLower(c.text), nil } + +FalseToken + = "FALSE"i !NormalIdentifierRest +{ return toLower(c.text), nil } + +NullToken + = "NULL"i !NormalIdentifierRest + +IsToken + = "IS"i !NormalIdentifierRest + +NullsToken + = "NULLS"i !NormalIdentifierRest + +LastToken + = "LAST"i !NormalIdentifierRest +{ return toLower(c.text), nil } + +FirstToken + = "FIRST"i !NormalIdentifierRest +{ return toLower(c.text), nil } + +AndToken + = "AND"i !NormalIdentifierRest + +OrToken + = "OR"i !NormalIdentifierRest + +NotToken + = "NOT"i !NormalIdentifierRest + +InToken + = "IN"i !NormalIdentifierRest + +LikeToken + = "LIKE"i !NormalIdentifierRest + +AscToken + = "ASC"i !NormalIdentifierRest +{ return toLower(c.text), nil } + +DescToken + = "DESC"i !NormalIdentifierRest +{ return toLower(c.text), nil } + +CastToken + = "CAST"i !NormalIdentifierRest + +AsToken + = "AS"i !NormalIdentifierRest + +SeparatorToken + = "," + +AnyToken + = "*" + +/* identifier */ +Identifier + = NormalIdentifier + / StringIdentifier + +NormalIdentifier + = NormalIdentifierStart NormalIdentifierRest* +{ return identifierNode(c.text), nil } + +NormalIdentifierStart + = [a-zA-Z@#_\u0080-\uffff] + +NormalIdentifierRest + = [a-zA-Z0-9@#$_\u0080-\uffff] + +StringIdentifier + = "\"" s:( ( [^"\r\n\\] / "\\" . ) { return c.text, nil } )* "\"" +{ + return identifierNode(resolveString(joinBytes(s))), nil +} + +/* skip */ +_ + = ( Whitespace / Newline )* + +Newline + = "\r\n" + / "\r" + / "\n" + +Whitespace + = " " + / "\t" + / "\v" + / "\f" + +EOF + = !. diff --git a/core/vm/sqlvm/parser.go b/core/vm/sqlvm/parser.go new file mode 100644 index 000000000..0b284c0e3 --- /dev/null +++ b/core/vm/sqlvm/parser.go @@ -0,0 +1,125 @@ +package sqlvm + +import ( + "encoding/hex" + "strconv" + "strings" + + "github.com/shopspring/decimal" +) + +// Parser was generated with pigeon v1.0.0-99-gbb0192c. +//go:generate pigeon -no-recover -o grammar.go grammar.peg +//go:generate goimports -w grammar.go + +func prepend(x interface{}, xs interface{}) []interface{} { + return append([]interface{}{x}, toSlice(xs)...) +} + +func toSlice(x interface{}) []interface{} { + if x == nil { + return nil + } + return x.([]interface{}) +} + +// TODO(wmin0): finish it. +func isAddress(h []byte) bool { + return false +} + +func hexToInteger(h []byte) interface{} { + d := decimal.Zero + l := len(h) + base := decimal.New(16, 0) + for idx, b := range h { + i, _ := strconv.ParseInt(string([]byte{b}), 16, 32) + d = d.Add( + decimal.New(i, 0). + Mul(base.Pow(decimal.New(int64(l-idx-1), 0))), + ) + } + return integerValueNode{v: d, address: isAddress(h)} +} + +func hexToBytes(h []byte) []byte { + bs, _ := hex.DecodeString(string(h)) + return bs +} + +func toInt(b []byte) int32 { + i, _ := strconv.ParseInt(string(b), 10, 32) + return int32(i) +} + +func toDecimal(b []byte) decimal.Decimal { + return decimal.RequireFromString(string(b)) +} + +func toLower(b []byte) []byte { + return []byte(strings.ToLower(string(b))) +} + +func joinBytes(x interface{}) []byte { + xs := toSlice(x) + bs := []byte{} + for _, b := range xs { + bs = append(bs, b.([]byte)...) + } + return bs +} + +func opSetSubject(op interface{}, s interface{}) interface{} { + x := op.(binaryOperator) + x.setSubject(s) + return x +} + +func opSetObject(op interface{}, o interface{}) interface{} { + x := op.(binaryOperator) + x.setObject(o) + return x +} + +func opSetTarget(op interface{}, t interface{}) interface{} { + x := op.(unaryOperator) + x.setTarget(t) + return x +} + +func joinOperator(x interface{}, o interface{}) { + if op, ok := x.(unaryOperator); ok { + joinOperator(op.getTarget(), o) + return + } + if op, ok := x.(binaryOperator); ok { + op.setObject(o) + return + } +} + +func rightJoinOperators(o interface{}, x interface{}) interface{} { + xs := toSlice(x) + if len(xs) == 0 { + return o + } + l := len(xs) + for idx := range xs { + if idx == l-1 { + break + } + joinOperator(xs[idx+1], xs[idx]) + } + joinOperator(xs[0], o) + return xs[l-1] +} + +// TODO(wmin0): finish it. +func resolveString(s []byte) []byte { + return s +} + +// ParseString parses input string to AST. +func ParseString(s string) (interface{}, error) { + return ParseReader("parser", strings.NewReader(s)) +} diff --git a/core/vm/sqlvm/parser_test.go b/core/vm/sqlvm/parser_test.go new file mode 100644 index 000000000..e7dc18e95 --- /dev/null +++ b/core/vm/sqlvm/parser_test.go @@ -0,0 +1,82 @@ +package sqlvm + +import ( + "testing" + + "github.com/stretchr/testify/suite" +) + +type ParserTestSuite struct{ suite.Suite } + +func (s *ParserTestSuite) requireParseNoError(sql string) { + _, err := ParseString(sql) + s.Require().NoError(err) +} + +func (s *ParserTestSuite) TestParse() { + // Test stmt. + s.requireParseNoError(``) + s.requireParseNoError(`;`) + s.requireParseNoError(`;;;select 1;;;;`) + + // Test expr. + s.requireParseNoError(`select 1 + 2 * 3`) + s.requireParseNoError(`select a(1 + 1)`) + s.requireParseNoError(`select hEx'12'`) + s.requireParseNoError(`select x'12'`) + s.requireParseNoError(`select 0xABC`) + s.requireParseNoError(`select true and false or true and false or true`) + s.requireParseNoError(`SeLeCT '1' NoT LiKe '1';`) + s.requireParseNoError(`select a in (1,2) is not null not in (true)`) + s.requireParseNoError(`select count(*)`) + s.requireParseNoError(`select cast(a as fixed65535X1)`) + s.requireParseNoError(`select "now!" ( not a + b, aa( + 3 + .1 + 1. ) + - .3e-9 + 1.e-10, 'a' || 'b' and true )`) + + // Test where. + s.requireParseNoError(`select * from abc where abc is null`) + s.requireParseNoError(`select * from abc where abc is not null`) + s.requireParseNoError(`select * from abc where abc in (1, 1)`) + s.requireParseNoError(`select * from abc where abc not in (1, 1)`) + s.requireParseNoError(`select * from abc where not true`) + s.requireParseNoError(`select * from abc where a like a + 1`) + + // Test some logic expr and no from. + s.requireParseNoError(`select 1 where a is not null = b`) + s.requireParseNoError(`select 1 where null = null is null and true`) + s.requireParseNoError(`select 1 where null is null = null`) + s.requireParseNoError(`SELECT 1 + 2 WHERE 3 <> 4`) + + // Test order by. + s.requireParseNoError(`select a=b+1 from a order by a desc`) + s.requireParseNoError(`select 1 from a order by b + 1 desc`) + s.requireParseNoError(`select 1 from a order by b + 1 nulls first`) + s.requireParseNoError(`select 1 from a order by b + 1 desc nulls last`) + + // Test group by. + s.requireParseNoError(`select 1 from a group by b + 1`) + + // Test insert. + s.requireParseNoError(`insert into "abc"(a) values (f(a, b),g(a),h())`) + s.requireParseNoError(`insert into "abc"(a) values (1,2,3), (f(a, b),g(a),h())`) + s.requireParseNoError(`insert into a default values`) + s.requireParseNoError(`insert into a values (default)`) + + // Test update. + s.requireParseNoError(`update "~!@#$%^&*()" set b = default where a is null;`) + s.requireParseNoError(`update "~!@#$%^&*()" set b = default, a = 123 where a is null;`) + + // Test delete. + s.requireParseNoError(`delete from a where b is null`) + + // Test create table. + s.requireParseNoError(`create table a (a int32 not null unique primary key default 0)`) + s.requireParseNoError(`create table "~!@#$%^&*()" ( a int32 references b ( a ) , b string primary key, c address not null default 1 + 1 )`) + + // Test create index. + s.requireParseNoError(`create unique index a on a (a)`) + s.requireParseNoError(`create index "~!@#$%^&*()" on ㄅ ( a , b )`) +} + +func TestParser(t *testing.T) { + suite.Run(t, new(ParserTestSuite)) +} diff --git a/core/vm/sqlvm/type.go b/core/vm/sqlvm/type.go new file mode 100644 index 000000000..890b5e6ee --- /dev/null +++ b/core/vm/sqlvm/type.go @@ -0,0 +1,408 @@ +package sqlvm + +import ( + "fmt" + "reflect" + + "github.com/shopspring/decimal" +) + +type identifierNode string + +type valuer interface { + value() interface{} +} + +type boolValueNode bool + +func (n boolValueNode) value() interface{} { return bool(n) } + +type integerValueNode struct { + v decimal.Decimal + address bool +} + +func (n integerValueNode) value() interface{} { return n.v } +func (n integerValueNode) String() string { return n.v.String() } + +type decimalValueNode decimal.Decimal + +func (n decimalValueNode) value() interface{} { return decimal.Decimal(n) } +func (n decimalValueNode) String() string { return decimal.Decimal(n).String() } + +type bytesValueNode []byte + +func (n bytesValueNode) value() interface{} { return []byte(n) } +func (n bytesValueNode) String() string { return string(n) } + +type anyValueNode struct{} + +func (n anyValueNode) value() interface{} { return n } + +type defaultValueNode struct{} + +func (n defaultValueNode) value() interface{} { return n } + +type nullValueNode struct{} + +func (n nullValueNode) value() interface{} { return n } + +type intTypeNode struct { + size int32 + unsigned bool +} + +type fixedTypeNode struct { + integerSize int32 + fractionalSize int32 + unsigned bool +} + +type dynamicBytesTypeNode struct{} + +type fixedBytesTypeNode struct { + size int32 +} + +type addressTypeNode struct{} +type boolTypeNode struct{} + +type unaryOperator interface { + getTarget() interface{} + setTarget(interface{}) +} + +type binaryOperator interface { + getObject() interface{} + getSubject() interface{} + setObject(interface{}) + setSubject(interface{}) +} + +type unaryOperatorNode struct { + target interface{} +} + +func (n unaryOperatorNode) getTarget() interface{} { + return n.target +} + +func (n *unaryOperatorNode) setTarget(t interface{}) { + n.target = t +} + +type binaryOperatorNode struct { + object interface{} + subject interface{} +} + +func (n binaryOperatorNode) getObject() interface{} { + return n.object +} + +func (n binaryOperatorNode) getSubject() interface{} { + return n.subject +} + +func (n *binaryOperatorNode) setObject(o interface{}) { + n.object = o +} + +func (n *binaryOperatorNode) setSubject(s interface{}) { + n.subject = s +} + +type posOperatorNode struct{ unaryOperatorNode } +type negOperatorNode struct{ unaryOperatorNode } +type notOperatorNode struct{ unaryOperatorNode } +type andOperatorNode struct{ binaryOperatorNode } +type orOperatorNode struct{ binaryOperatorNode } + +type greaterOrEqualOperatorNode struct{ binaryOperatorNode } +type lessOrEqualOperatorNode struct{ binaryOperatorNode } +type notEqualOperatorNode struct{ binaryOperatorNode } +type equalOperatorNode struct{ binaryOperatorNode } +type greaterOperatorNode struct{ binaryOperatorNode } +type lessOperatorNode struct{ binaryOperatorNode } + +type concatOperatorNode struct{ binaryOperatorNode } +type addOperatorNode struct{ binaryOperatorNode } +type subOperatorNode struct{ binaryOperatorNode } +type mulOperatorNode struct{ binaryOperatorNode } +type divOperatorNode struct{ binaryOperatorNode } +type modOperatorNode struct{ binaryOperatorNode } + +type inOperatorNode struct{ binaryOperatorNode } +type isOperatorNode struct{ binaryOperatorNode } +type likeOperatorNode struct{ binaryOperatorNode } + +type castOperatorNode struct{ binaryOperatorNode } +type assignOperatorNode struct{ binaryOperatorNode } +type functionOperatorNode struct{ binaryOperatorNode } + +type optional interface { + getOption() map[string]interface{} +} + +type nilOptionNode struct{} + +func (n nilOptionNode) getOption() map[string]interface{} { return nil } + +type whereOptionNode struct { + condition interface{} +} + +func (n whereOptionNode) getOption() map[string]interface{} { + return map[string]interface{}{ + "condition": n.condition, + } +} + +type orderOptionNode struct { + expr interface{} + desc bool + nullfirst bool +} + +func (n orderOptionNode) getOption() map[string]interface{} { + return map[string]interface{}{ + "expr": n.expr, + "desc": n.desc, + "nullfirst": n.nullfirst, + } +} + +type groupOptionNode struct { + expr interface{} +} + +func (n groupOptionNode) getOption() map[string]interface{} { + return map[string]interface{}{ + "expr": n.expr, + } +} + +type offsetOptionNode struct { + value integerValueNode +} + +func (n offsetOptionNode) getOption() map[string]interface{} { + return map[string]interface{}{ + "value": n.value, + } +} + +type limitOptionNode struct { + value integerValueNode +} + +func (n limitOptionNode) getOption() map[string]interface{} { + return map[string]interface{}{ + "value": n.value, + } +} + +type insertWithColumnOptionNode struct { + column []interface{} + value []interface{} +} + +func (n insertWithColumnOptionNode) getOption() map[string]interface{} { + return map[string]interface{}{ + "column": n.column, + "value": n.value, + } +} + +type insertWithDefaultOptionNode struct{ nilOptionNode } +type primaryOptionNode struct{ nilOptionNode } +type notNullOptionNode struct{ nilOptionNode } +type uniqueOptionNode struct{ nilOptionNode } +type autoincrementOptionNode struct{ nilOptionNode } + +type defaultOptionNode struct { + value interface{} +} + +func (n defaultOptionNode) getOption() map[string]interface{} { + return map[string]interface{}{ + "value": n.value, + } +} + +type foreignOptionNode struct { + table identifierNode + column identifierNode +} + +func (n foreignOptionNode) getOption() map[string]interface{} { + return map[string]interface{}{ + "table": n.table, + "column": n.column, + } +} + +type selectStmtNode struct { + column []interface{} + table *identifierNode + where *whereOptionNode + group []interface{} + order []interface{} + limit *limitOptionNode + offset *offsetOptionNode +} + +func (n selectStmtNode) getOption() map[string]interface{} { + return map[string]interface{}{ + "column": n.column, + "table": n.table, + "where": n.where, + "group": n.group, + "order": n.order, + "limit": n.limit, + "offset": n.offset, + } +} + +type updateStmtNode struct { + table identifierNode + assignment []interface{} + where *whereOptionNode +} + +func (n updateStmtNode) getOption() map[string]interface{} { + return map[string]interface{}{ + "table": n.table, + "assignment": n.assignment, + "where": n.where, + } +} + +type deleteStmtNode struct { + table identifierNode + where *whereOptionNode +} + +func (n deleteStmtNode) getOption() map[string]interface{} { + return map[string]interface{}{ + "table": n.table, + "where": n.where, + } +} + +type insertStmtNode struct { + table identifierNode + insert interface{} +} + +func (n insertStmtNode) getOption() map[string]interface{} { + return map[string]interface{}{ + "table": n.table, + "insert": n.insert, + } +} + +type createTableStmtNode struct { + table identifierNode + column []interface{} +} + +func (n createTableStmtNode) getOption() map[string]interface{} { + return map[string]interface{}{ + "table": n.table, + "column": n.column, + } +} + +type columnSchemaNode struct { + column identifierNode + dataType interface{} + constraint []interface{} +} + +func (n columnSchemaNode) getOption() map[string]interface{} { + return map[string]interface{}{ + "column": n.column, + "data_type": n.dataType, + "constraint": n.constraint, + } +} + +type createIndexStmtNode struct { + index identifierNode + table identifierNode + column []interface{} + unique *uniqueOptionNode +} + +func (n createIndexStmtNode) getOption() map[string]interface{} { + return map[string]interface{}{ + "index": n.index, + "table": n.table, + "column": n.column, + "unique": n.unique, + } +} + +// PrintAST prints ast to stdout. +func PrintAST(n interface{}, indent string) { + if n == nil { + fmt.Printf("%snil\n", indent) + return + } + typeOf := reflect.TypeOf(n) + valueOf := reflect.ValueOf(n) + name := "" + if typeOf.Kind() == reflect.Ptr { + if valueOf.IsNil() { + fmt.Printf("%snil\n", indent) + return + } + name = "*" + valueOf = valueOf.Elem() + typeOf = typeOf.Elem() + } + name = name + typeOf.Name() + + if op, ok := n.(optional); ok { + fmt.Printf("%s%s", indent, name) + m := op.getOption() + if m == nil { + fmt.Printf("\n") + return + } + fmt.Printf(":\n") + for k := range m { + fmt.Printf("%s %s:\n", indent, k) + PrintAST(m[k], indent+" ") + } + return + } + if op, ok := n.(unaryOperator); ok { + fmt.Printf("%s%s:\n", indent, name) + fmt.Printf("%s target:\n", indent) + PrintAST(op.getTarget(), indent+" ") + return + } + if op, ok := n.(binaryOperator); ok { + fmt.Printf("%s%s:\n", indent, name) + fmt.Printf("%s object:\n", indent) + PrintAST(op.getObject(), indent+" ") + fmt.Printf("%s subject:\n", indent) + PrintAST(op.getSubject(), indent+" ") + return + } + if arr, ok := n.([]interface{}); ok { + fmt.Printf("%s[\n", indent) + for idx := range arr { + PrintAST(arr[idx], indent+" ") + } + fmt.Printf("%s]\n", indent) + return + } + if stringer, ok := n.(fmt.Stringer); ok { + fmt.Printf("%s%s: %s\n", indent, name, stringer.String()) + return + } + fmt.Printf("%s%s: %+v\n", indent, name, valueOf.Interface()) +} diff --git a/vendor/github.com/shopspring/decimal/LICENSE b/vendor/github.com/shopspring/decimal/LICENSE new file mode 100644 index 000000000..ad2148aaf --- /dev/null +++ b/vendor/github.com/shopspring/decimal/LICENSE @@ -0,0 +1,45 @@ +The MIT License (MIT) + +Copyright (c) 2015 Spring, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +- Based on https://github.com/oguzbilgic/fpd, which has the following license: +""" +The MIT License (MIT) + +Copyright (c) 2013 Oguz Bilgic + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +""" diff --git a/vendor/github.com/shopspring/decimal/README.md b/vendor/github.com/shopspring/decimal/README.md new file mode 100644 index 000000000..54c1a6b4a --- /dev/null +++ b/vendor/github.com/shopspring/decimal/README.md @@ -0,0 +1,126 @@ +# decimal + +[![Build Status](https://travis-ci.org/shopspring/decimal.png?branch=master)](https://travis-ci.org/shopspring/decimal) [![GoDoc](https://godoc.org/github.com/shopspring/decimal?status.svg)](https://godoc.org/github.com/shopspring/decimal) [![Go Report Card](https://goreportcard.com/badge/github.com/shopspring/decimal)](https://goreportcard.com/report/github.com/shopspring/decimal) + +Arbitrary-precision fixed-point decimal numbers in go. + +NOTE: can "only" represent numbers with a maximum of 2^31 digits after the decimal point. + +## Features + + * the zero-value is 0, and is safe to use without initialization + * addition, subtraction, multiplication with no loss of precision + * division with specified precision + * database/sql serialization/deserialization + * json and xml serialization/deserialization + +## Install + +Run `go get github.com/shopspring/decimal` + +## Usage + +```go +package main + +import ( + "fmt" + "github.com/shopspring/decimal" +) + +func main() { + price, err := decimal.NewFromString("136.02") + if err != nil { + panic(err) + } + + quantity := decimal.NewFromFloat(3) + + fee, _ := decimal.NewFromString(".035") + taxRate, _ := decimal.NewFromString(".08875") + + subtotal := price.Mul(quantity) + + preTax := subtotal.Mul(fee.Add(decimal.NewFromFloat(1))) + + total := preTax.Mul(taxRate.Add(decimal.NewFromFloat(1))) + + fmt.Println("Subtotal:", subtotal) // Subtotal: 408.06 + fmt.Println("Pre-tax:", preTax) // Pre-tax: 422.3421 + fmt.Println("Taxes:", total.Sub(preTax)) // Taxes: 37.482861375 + fmt.Println("Total:", total) // Total: 459.824961375 + fmt.Println("Tax rate:", total.Sub(preTax).Div(preTax)) // Tax rate: 0.08875 +} +``` + +## Documentation + +http://godoc.org/github.com/shopspring/decimal + +## Production Usage + +* [Spring](https://shopspring.com/), since August 14, 2014. +* If you are using this in production, please let us know! + +## FAQ + +#### Why don't you just use float64? + +Because float64s (or any binary floating point type, actually) can't represent +numbers such as 0.1 exactly. + +Consider this code: http://play.golang.org/p/TQBd4yJe6B You might expect that +it prints out `10`, but it actually prints `9.999999999999831`. Over time, +these small errors can really add up! + +#### Why don't you just use big.Rat? + +big.Rat is fine for representing rational numbers, but Decimal is better for +representing money. Why? Here's a (contrived) example: + +Let's say you use big.Rat, and you have two numbers, x and y, both +representing 1/3, and you have `z = 1 - x - y = 1/3`. If you print each one +out, the string output has to stop somewhere (let's say it stops at 3 decimal +digits, for simplicity), so you'll get 0.333, 0.333, and 0.333. But where did +the other 0.001 go? + +Here's the above example as code: http://play.golang.org/p/lCZZs0w9KE + +With Decimal, the strings being printed out represent the number exactly. So, +if you have `x = y = 1/3` (with precision 3), they will actually be equal to +0.333, and when you do `z = 1 - x - y`, `z` will be equal to .334. No money is +unaccounted for! + +You still have to be careful. If you want to split a number `N` 3 ways, you +can't just send `N/3` to three different people. You have to pick one to send +`N - (2/3*N)` to. That person will receive the fraction of a penny remainder. + +But, it is much easier to be careful with Decimal than with big.Rat. + +#### Why isn't the API similar to big.Int's? + +big.Int's API is built to reduce the number of memory allocations for maximal +performance. This makes sense for its use-case, but the trade-off is that the +API is awkward and easy to misuse. + +For example, to add two big.Ints, you do: `z := new(big.Int).Add(x, y)`. A +developer unfamiliar with this API might try to do `z := a.Add(a, b)`. This +modifies `a` and sets `z` as an alias for `a`, which they might not expect. It +also modifies any other aliases to `a`. + +Here's an example of the subtle bugs you can introduce with big.Int's API: +https://play.golang.org/p/x2R_78pa8r + +In contrast, it's difficult to make such mistakes with decimal. Decimals +behave like other go numbers types: even though `a = b` will not deep copy +`b` into `a`, it is impossible to modify a Decimal, since all Decimal methods +return new Decimals and do not modify the originals. The downside is that +this causes extra allocations, so Decimal is less performant. My assumption +is that if you're using Decimals, you probably care more about correctness +than performance. + +## License + +The MIT License (MIT) + +This is a heavily modified fork of [fpd.Decimal](https://github.com/oguzbilgic/fpd), which was also released under the MIT License. diff --git a/vendor/github.com/shopspring/decimal/decimal-go.go b/vendor/github.com/shopspring/decimal/decimal-go.go new file mode 100644 index 000000000..e08a15ce4 --- /dev/null +++ b/vendor/github.com/shopspring/decimal/decimal-go.go @@ -0,0 +1,414 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Multiprecision decimal numbers. +// For floating-point formatting only; not general purpose. +// Only operations are assign and (binary) left/right shift. +// Can do binary floating point in multiprecision decimal precisely +// because 2 divides 10; cannot do decimal floating point +// in multiprecision binary precisely. +package decimal + +type decimal struct { + d [800]byte // digits, big-endian representation + nd int // number of digits used + dp int // decimal point + neg bool // negative flag + trunc bool // discarded nonzero digits beyond d[:nd] +} + +func (a *decimal) String() string { + n := 10 + a.nd + if a.dp > 0 { + n += a.dp + } + if a.dp < 0 { + n += -a.dp + } + + buf := make([]byte, n) + w := 0 + switch { + case a.nd == 0: + return "0" + + case a.dp <= 0: + // zeros fill space between decimal point and digits + buf[w] = '0' + w++ + buf[w] = '.' + w++ + w += digitZero(buf[w : w+-a.dp]) + w += copy(buf[w:], a.d[0:a.nd]) + + case a.dp < a.nd: + // decimal point in middle of digits + w += copy(buf[w:], a.d[0:a.dp]) + buf[w] = '.' + w++ + w += copy(buf[w:], a.d[a.dp:a.nd]) + + default: + // zeros fill space between digits and decimal point + w += copy(buf[w:], a.d[0:a.nd]) + w += digitZero(buf[w : w+a.dp-a.nd]) + } + return string(buf[0:w]) +} + +func digitZero(dst []byte) int { + for i := range dst { + dst[i] = '0' + } + return len(dst) +} + +// trim trailing zeros from number. +// (They are meaningless; the decimal point is tracked +// independent of the number of digits.) +func trim(a *decimal) { + for a.nd > 0 && a.d[a.nd-1] == '0' { + a.nd-- + } + if a.nd == 0 { + a.dp = 0 + } +} + +// Assign v to a. +func (a *decimal) Assign(v uint64) { + var buf [24]byte + + // Write reversed decimal in buf. + n := 0 + for v > 0 { + v1 := v / 10 + v -= 10 * v1 + buf[n] = byte(v + '0') + n++ + v = v1 + } + + // Reverse again to produce forward decimal in a.d. + a.nd = 0 + for n--; n >= 0; n-- { + a.d[a.nd] = buf[n] + a.nd++ + } + a.dp = a.nd + trim(a) +} + +// Maximum shift that we can do in one pass without overflow. +// A uint has 32 or 64 bits, and we have to be able to accommodate 9<<k. +const uintSize = 32 << (^uint(0) >> 63) +const maxShift = uintSize - 4 + +// Binary shift right (/ 2) by k bits. k <= maxShift to avoid overflow. +func rightShift(a *decimal, k uint) { + r := 0 // read pointer + w := 0 // write pointer + + // Pick up enough leading digits to cover first shift. + var n uint + for ; n>>k == 0; r++ { + if r >= a.nd { + if n == 0 { + // a == 0; shouldn't get here, but handle anyway. + a.nd = 0 + return + } + for n>>k == 0 { + n = n * 10 + r++ + } + break + } + c := uint(a.d[r]) + n = n*10 + c - '0' + } + a.dp -= r - 1 + + var mask uint = (1 << k) - 1 + + // Pick up a digit, put down a digit. + for ; r < a.nd; r++ { + c := uint(a.d[r]) + dig := n >> k + n &= mask + a.d[w] = byte(dig + '0') + w++ + n = n*10 + c - '0' + } + + // Put down extra digits. + for n > 0 { + dig := n >> k + n &= mask + if w < len(a.d) { + a.d[w] = byte(dig + '0') + w++ + } else if dig > 0 { + a.trunc = true + } + n = n * 10 + } + + a.nd = w + trim(a) +} + +// Cheat sheet for left shift: table indexed by shift count giving +// number of new digits that will be introduced by that shift. +// +// For example, leftcheats[4] = {2, "625"}. That means that +// if we are shifting by 4 (multiplying by 16), it will add 2 digits +// when the string prefix is "625" through "999", and one fewer digit +// if the string prefix is "000" through "624". +// +// Credit for this trick goes to Ken. + +type leftCheat struct { + delta int // number of new digits + cutoff string // minus one digit if original < a. +} + +var leftcheats = []leftCheat{ + // Leading digits of 1/2^i = 5^i. + // 5^23 is not an exact 64-bit floating point number, + // so have to use bc for the math. + // Go up to 60 to be large enough for 32bit and 64bit platforms. + /* + seq 60 | sed 's/^/5^/' | bc | + awk 'BEGIN{ print "\t{ 0, \"\" }," } + { + log2 = log(2)/log(10) + printf("\t{ %d, \"%s\" },\t// * %d\n", + int(log2*NR+1), $0, 2**NR) + }' + */ + {0, ""}, + {1, "5"}, // * 2 + {1, "25"}, // * 4 + {1, "125"}, // * 8 + {2, "625"}, // * 16 + {2, "3125"}, // * 32 + {2, "15625"}, // * 64 + {3, "78125"}, // * 128 + {3, "390625"}, // * 256 + {3, "1953125"}, // * 512 + {4, "9765625"}, // * 1024 + {4, "48828125"}, // * 2048 + {4, "244140625"}, // * 4096 + {4, "1220703125"}, // * 8192 + {5, "6103515625"}, // * 16384 + {5, "30517578125"}, // * 32768 + {5, "152587890625"}, // * 65536 + {6, "762939453125"}, // * 131072 + {6, "3814697265625"}, // * 262144 + {6, "19073486328125"}, // * 524288 + {7, "95367431640625"}, // * 1048576 + {7, "476837158203125"}, // * 2097152 + {7, "2384185791015625"}, // * 4194304 + {7, "11920928955078125"}, // * 8388608 + {8, "59604644775390625"}, // * 16777216 + {8, "298023223876953125"}, // * 33554432 + {8, "1490116119384765625"}, // * 67108864 + {9, "7450580596923828125"}, // * 134217728 + {9, "37252902984619140625"}, // * 268435456 + {9, "186264514923095703125"}, // * 536870912 + {10, "931322574615478515625"}, // * 1073741824 + {10, "4656612873077392578125"}, // * 2147483648 + {10, "23283064365386962890625"}, // * 4294967296 + {10, "116415321826934814453125"}, // * 8589934592 + {11, "582076609134674072265625"}, // * 17179869184 + {11, "2910383045673370361328125"}, // * 34359738368 + {11, "14551915228366851806640625"}, // * 68719476736 + {12, "72759576141834259033203125"}, // * 137438953472 + {12, "363797880709171295166015625"}, // * 274877906944 + {12, "1818989403545856475830078125"}, // * 549755813888 + {13, "9094947017729282379150390625"}, // * 1099511627776 + {13, "45474735088646411895751953125"}, // * 2199023255552 + {13, "227373675443232059478759765625"}, // * 4398046511104 + {13, "1136868377216160297393798828125"}, // * 8796093022208 + {14, "5684341886080801486968994140625"}, // * 17592186044416 + {14, "28421709430404007434844970703125"}, // * 35184372088832 + {14, "142108547152020037174224853515625"}, // * 70368744177664 + {15, "710542735760100185871124267578125"}, // * 140737488355328 + {15, "3552713678800500929355621337890625"}, // * 281474976710656 + {15, "17763568394002504646778106689453125"}, // * 562949953421312 + {16, "88817841970012523233890533447265625"}, // * 1125899906842624 + {16, "444089209850062616169452667236328125"}, // * 2251799813685248 + {16, "2220446049250313080847263336181640625"}, // * 4503599627370496 + {16, "11102230246251565404236316680908203125"}, // * 9007199254740992 + {17, "55511151231257827021181583404541015625"}, // * 18014398509481984 + {17, "277555756156289135105907917022705078125"}, // * 36028797018963968 + {17, "1387778780781445675529539585113525390625"}, // * 72057594037927936 + {18, "6938893903907228377647697925567626953125"}, // * 144115188075855872 + {18, "34694469519536141888238489627838134765625"}, // * 288230376151711744 + {18, "173472347597680709441192448139190673828125"}, // * 576460752303423488 + {19, "867361737988403547205962240695953369140625"}, // * 1152921504606846976 +} + +// Is the leading prefix of b lexicographically less than s? +func prefixIsLessThan(b []byte, s string) bool { + for i := 0; i < len(s); i++ { + if i >= len(b) { + return true + } + if b[i] != s[i] { + return b[i] < s[i] + } + } + return false +} + +// Binary shift left (* 2) by k bits. k <= maxShift to avoid overflow. +func leftShift(a *decimal, k uint) { + delta := leftcheats[k].delta + if prefixIsLessThan(a.d[0:a.nd], leftcheats[k].cutoff) { + delta-- + } + + r := a.nd // read index + w := a.nd + delta // write index + + // Pick up a digit, put down a digit. + var n uint + for r--; r >= 0; r-- { + n += (uint(a.d[r]) - '0') << k + quo := n / 10 + rem := n - 10*quo + w-- + if w < len(a.d) { + a.d[w] = byte(rem + '0') + } else if rem != 0 { + a.trunc = true + } + n = quo + } + + // Put down extra digits. + for n > 0 { + quo := n / 10 + rem := n - 10*quo + w-- + if w < len(a.d) { + a.d[w] = byte(rem + '0') + } else if rem != 0 { + a.trunc = true + } + n = quo + } + + a.nd += delta + if a.nd >= len(a.d) { + a.nd = len(a.d) + } + a.dp += delta + trim(a) +} + +// Binary shift left (k > 0) or right (k < 0). +func (a *decimal) Shift(k int) { + switch { + case a.nd == 0: + // nothing to do: a == 0 + case k > 0: + for k > maxShift { + leftShift(a, maxShift) + k -= maxShift + } + leftShift(a, uint(k)) + case k < 0: + for k < -maxShift { + rightShift(a, maxShift) + k += maxShift + } + rightShift(a, uint(-k)) + } +} + +// If we chop a at nd digits, should we round up? +func shouldRoundUp(a *decimal, nd int) bool { + if nd < 0 || nd >= a.nd { + return false + } + if a.d[nd] == '5' && nd+1 == a.nd { // exactly halfway - round to even + // if we truncated, a little higher than what's recorded - always round up + if a.trunc { + return true + } + return nd > 0 && (a.d[nd-1]-'0')%2 != 0 + } + // not halfway - digit tells all + return a.d[nd] >= '5' +} + +// Round a to nd digits (or fewer). +// If nd is zero, it means we're rounding +// just to the left of the digits, as in +// 0.09 -> 0.1. +func (a *decimal) Round(nd int) { + if nd < 0 || nd >= a.nd { + return + } + if shouldRoundUp(a, nd) { + a.RoundUp(nd) + } else { + a.RoundDown(nd) + } +} + +// Round a down to nd digits (or fewer). +func (a *decimal) RoundDown(nd int) { + if nd < 0 || nd >= a.nd { + return + } + a.nd = nd + trim(a) +} + +// Round a up to nd digits (or fewer). +func (a *decimal) RoundUp(nd int) { + if nd < 0 || nd >= a.nd { + return + } + + // round up + for i := nd - 1; i >= 0; i-- { + c := a.d[i] + if c < '9' { // can stop after this digit + a.d[i]++ + a.nd = i + 1 + return + } + } + + // Number is all 9s. + // Change to single 1 with adjusted decimal point. + a.d[0] = '1' + a.nd = 1 + a.dp++ +} + +// Extract integer part, rounded appropriately. +// No guarantees about overflow. +func (a *decimal) RoundedInteger() uint64 { + if a.dp > 20 { + return 0xFFFFFFFFFFFFFFFF + } + var i int + n := uint64(0) + for i = 0; i < a.dp && i < a.nd; i++ { + n = n*10 + uint64(a.d[i]-'0') + } + for ; i < a.dp; i++ { + n *= 10 + } + if shouldRoundUp(a, a.dp) { + n++ + } + return n +} diff --git a/vendor/github.com/shopspring/decimal/decimal.go b/vendor/github.com/shopspring/decimal/decimal.go new file mode 100644 index 000000000..134ece2ff --- /dev/null +++ b/vendor/github.com/shopspring/decimal/decimal.go @@ -0,0 +1,1434 @@ +// Package decimal implements an arbitrary precision fixed-point decimal. +// +// To use as part of a struct: +// +// type Struct struct { +// Number Decimal +// } +// +// The zero-value of a Decimal is 0, as you would expect. +// +// The best way to create a new Decimal is to use decimal.NewFromString, ex: +// +// n, err := decimal.NewFromString("-123.4567") +// n.String() // output: "-123.4567" +// +// NOTE: This can "only" represent numbers with a maximum of 2^31 digits +// after the decimal point. +package decimal + +import ( + "database/sql/driver" + "encoding/binary" + "fmt" + "math" + "math/big" + "strconv" + "strings" +) + +// DivisionPrecision is the number of decimal places in the result when it +// doesn't divide exactly. +// +// Example: +// +// d1 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3) +// d1.String() // output: "0.6666666666666667" +// d2 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(30000) +// d2.String() // output: "0.0000666666666667" +// d3 := decimal.NewFromFloat(20000).Div(decimal.NewFromFloat(3) +// d3.String() // output: "6666.6666666666666667" +// decimal.DivisionPrecision = 3 +// d4 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3) +// d4.String() // output: "0.667" +// +var DivisionPrecision = 16 + +// MarshalJSONWithoutQuotes should be set to true if you want the decimal to +// be JSON marshaled as a number, instead of as a string. +// WARNING: this is dangerous for decimals with many digits, since many JSON +// unmarshallers (ex: Javascript's) will unmarshal JSON numbers to IEEE 754 +// double-precision floating point numbers, which means you can potentially +// silently lose precision. +var MarshalJSONWithoutQuotes = false + +// Zero constant, to make computations faster. +var Zero = New(0, 1) + +// fiveDec used in Cash Rounding +var fiveDec = New(5, 0) + +var zeroInt = big.NewInt(0) +var oneInt = big.NewInt(1) +var twoInt = big.NewInt(2) +var fourInt = big.NewInt(4) +var fiveInt = big.NewInt(5) +var tenInt = big.NewInt(10) +var twentyInt = big.NewInt(20) + +// Decimal represents a fixed-point decimal. It is immutable. +// number = value * 10 ^ exp +type Decimal struct { + value *big.Int + + // NOTE(vadim): this must be an int32, because we cast it to float64 during + // calculations. If exp is 64 bit, we might lose precision. + // If we cared about being able to represent every possible decimal, we + // could make exp a *big.Int but it would hurt performance and numbers + // like that are unrealistic. + exp int32 +} + +// New returns a new fixed-point decimal, value * 10 ^ exp. +func New(value int64, exp int32) Decimal { + return Decimal{ + value: big.NewInt(value), + exp: exp, + } +} + +// NewFromBigInt returns a new Decimal from a big.Int, value * 10 ^ exp +func NewFromBigInt(value *big.Int, exp int32) Decimal { + return Decimal{ + value: big.NewInt(0).Set(value), + exp: exp, + } +} + +// NewFromString returns a new Decimal from a string representation. +// +// Example: +// +// d, err := NewFromString("-123.45") +// d2, err := NewFromString(".0001") +// +func NewFromString(value string) (Decimal, error) { + originalInput := value + var intString string + var exp int64 + + // Check if number is using scientific notation + eIndex := strings.IndexAny(value, "Ee") + if eIndex != -1 { + expInt, err := strconv.ParseInt(value[eIndex+1:], 10, 32) + if err != nil { + if e, ok := err.(*strconv.NumError); ok && e.Err == strconv.ErrRange { + return Decimal{}, fmt.Errorf("can't convert %s to decimal: fractional part too long", value) + } + return Decimal{}, fmt.Errorf("can't convert %s to decimal: exponent is not numeric", value) + } + value = value[:eIndex] + exp = expInt + } + + parts := strings.Split(value, ".") + if len(parts) == 1 { + // There is no decimal point, we can just parse the original string as + // an int + intString = value + } else if len(parts) == 2 { + // strip the insignificant digits for more accurate comparisons. + decimalPart := strings.TrimRight(parts[1], "0") + intString = parts[0] + decimalPart + expInt := -len(decimalPart) + exp += int64(expInt) + } else { + return Decimal{}, fmt.Errorf("can't convert %s to decimal: too many .s", value) + } + + dValue := new(big.Int) + _, ok := dValue.SetString(intString, 10) + if !ok { + return Decimal{}, fmt.Errorf("can't convert %s to decimal", value) + } + + if exp < math.MinInt32 || exp > math.MaxInt32 { + // NOTE(vadim): I doubt a string could realistically be this long + return Decimal{}, fmt.Errorf("can't convert %s to decimal: fractional part too long", originalInput) + } + + return Decimal{ + value: dValue, + exp: int32(exp), + }, nil +} + +// RequireFromString returns a new Decimal from a string representation +// or panics if NewFromString would have returned an error. +// +// Example: +// +// d := RequireFromString("-123.45") +// d2 := RequireFromString(".0001") +// +func RequireFromString(value string) Decimal { + dec, err := NewFromString(value) + if err != nil { + panic(err) + } + return dec +} + +// NewFromFloat converts a float64 to Decimal. +// +// The converted number will contain the number of significant digits that can be +// represented in a float with reliable roundtrip. +// This is typically 15 digits, but may be more in some cases. +// See https://www.exploringbinary.com/decimal-precision-of-binary-floating-point-numbers/ for more information. +// +// For slightly faster conversion, use NewFromFloatWithExponent where you can specify the precision in absolute terms. +// +// NOTE: this will panic on NaN, +/-inf +func NewFromFloat(value float64) Decimal { + if value == 0 { + return New(0, 0) + } + return newFromFloat(value, math.Float64bits(value), &float64info) +} + +// NewFromFloat converts a float32 to Decimal. +// +// The converted number will contain the number of significant digits that can be +// represented in a float with reliable roundtrip. +// This is typically 6-8 digits depending on the input. +// See https://www.exploringbinary.com/decimal-precision-of-binary-floating-point-numbers/ for more information. +// +// For slightly faster conversion, use NewFromFloatWithExponent where you can specify the precision in absolute terms. +// +// NOTE: this will panic on NaN, +/-inf +func NewFromFloat32(value float32) Decimal { + if value == 0 { + return New(0, 0) + } + // XOR is workaround for https://github.com/golang/go/issues/26285 + a := math.Float32bits(value) ^ 0x80808080 + return newFromFloat(float64(value), uint64(a)^0x80808080, &float32info) +} + +func newFromFloat(val float64, bits uint64, flt *floatInfo) Decimal { + if math.IsNaN(val) || math.IsInf(val, 0) { + panic(fmt.Sprintf("Cannot create a Decimal from %v", val)) + } + exp := int(bits>>flt.mantbits) & (1<<flt.expbits - 1) + mant := bits & (uint64(1)<<flt.mantbits - 1) + + switch exp { + case 0: + // denormalized + exp++ + + default: + // add implicit top bit + mant |= uint64(1) << flt.mantbits + } + exp += flt.bias + + var d decimal + d.Assign(mant) + d.Shift(exp - int(flt.mantbits)) + d.neg = bits>>(flt.expbits+flt.mantbits) != 0 + + roundShortest(&d, mant, exp, flt) + // If less than 19 digits, we can do calculation in an int64. + if d.nd < 19 { + tmp := int64(0) + m := int64(1) + for i := d.nd - 1; i >= 0; i-- { + tmp += m * int64(d.d[i]-'0') + m *= 10 + } + if d.neg { + tmp *= -1 + } + return Decimal{value: big.NewInt(tmp), exp: int32(d.dp) - int32(d.nd)} + } + dValue := new(big.Int) + dValue, ok := dValue.SetString(string(d.d[:d.nd]), 10) + if ok { + return Decimal{value: dValue, exp: int32(d.dp) - int32(d.nd)} + } + + return NewFromFloatWithExponent(val, int32(d.dp)-int32(d.nd)) +} + +// NewFromFloatWithExponent converts a float64 to Decimal, with an arbitrary +// number of fractional digits. +// +// Example: +// +// NewFromFloatWithExponent(123.456, -2).String() // output: "123.46" +// +func NewFromFloatWithExponent(value float64, exp int32) Decimal { + if math.IsNaN(value) || math.IsInf(value, 0) { + panic(fmt.Sprintf("Cannot create a Decimal from %v", value)) + } + + bits := math.Float64bits(value) + mant := bits & (1<<52 - 1) + exp2 := int32((bits >> 52) & (1<<11 - 1)) + sign := bits >> 63 + + if exp2 == 0 { + // specials + if mant == 0 { + return Decimal{} + } else { + // subnormal + exp2++ + } + } else { + // normal + mant |= 1 << 52 + } + + exp2 -= 1023 + 52 + + // normalizing base-2 values + for mant&1 == 0 { + mant = mant >> 1 + exp2++ + } + + // maximum number of fractional base-10 digits to represent 2^N exactly cannot be more than -N if N<0 + if exp < 0 && exp < exp2 { + if exp2 < 0 { + exp = exp2 + } else { + exp = 0 + } + } + + // representing 10^M * 2^N as 5^M * 2^(M+N) + exp2 -= exp + + temp := big.NewInt(1) + dMant := big.NewInt(int64(mant)) + + // applying 5^M + if exp > 0 { + temp = temp.SetInt64(int64(exp)) + temp = temp.Exp(fiveInt, temp, nil) + } else if exp < 0 { + temp = temp.SetInt64(-int64(exp)) + temp = temp.Exp(fiveInt, temp, nil) + dMant = dMant.Mul(dMant, temp) + temp = temp.SetUint64(1) + } + + // applying 2^(M+N) + if exp2 > 0 { + dMant = dMant.Lsh(dMant, uint(exp2)) + } else if exp2 < 0 { + temp = temp.Lsh(temp, uint(-exp2)) + } + + // rounding and downscaling + if exp > 0 || exp2 < 0 { + halfDown := new(big.Int).Rsh(temp, 1) + dMant = dMant.Add(dMant, halfDown) + dMant = dMant.Quo(dMant, temp) + } + + if sign == 1 { + dMant = dMant.Neg(dMant) + } + + return Decimal{ + value: dMant, + exp: exp, + } +} + +// rescale returns a rescaled version of the decimal. Returned +// decimal may be less precise if the given exponent is bigger +// than the initial exponent of the Decimal. +// NOTE: this will truncate, NOT round +// +// Example: +// +// d := New(12345, -4) +// d2 := d.rescale(-1) +// d3 := d2.rescale(-4) +// println(d1) +// println(d2) +// println(d3) +// +// Output: +// +// 1.2345 +// 1.2 +// 1.2000 +// +func (d Decimal) rescale(exp int32) Decimal { + d.ensureInitialized() + // NOTE(vadim): must convert exps to float64 before - to prevent overflow + diff := math.Abs(float64(exp) - float64(d.exp)) + value := new(big.Int).Set(d.value) + + expScale := new(big.Int).Exp(tenInt, big.NewInt(int64(diff)), nil) + if exp > d.exp { + value = value.Quo(value, expScale) + } else if exp < d.exp { + value = value.Mul(value, expScale) + } + + return Decimal{ + value: value, + exp: exp, + } +} + +// Abs returns the absolute value of the decimal. +func (d Decimal) Abs() Decimal { + d.ensureInitialized() + d2Value := new(big.Int).Abs(d.value) + return Decimal{ + value: d2Value, + exp: d.exp, + } +} + +// Add returns d + d2. +func (d Decimal) Add(d2 Decimal) Decimal { + baseScale := min(d.exp, d2.exp) + rd := d.rescale(baseScale) + rd2 := d2.rescale(baseScale) + + d3Value := new(big.Int).Add(rd.value, rd2.value) + return Decimal{ + value: d3Value, + exp: baseScale, + } +} + +// Sub returns d - d2. +func (d Decimal) Sub(d2 Decimal) Decimal { + baseScale := min(d.exp, d2.exp) + rd := d.rescale(baseScale) + rd2 := d2.rescale(baseScale) + + d3Value := new(big.Int).Sub(rd.value, rd2.value) + return Decimal{ + value: d3Value, + exp: baseScale, + } +} + +// Neg returns -d. +func (d Decimal) Neg() Decimal { + d.ensureInitialized() + val := new(big.Int).Neg(d.value) + return Decimal{ + value: val, + exp: d.exp, + } +} + +// Mul returns d * d2. +func (d Decimal) Mul(d2 Decimal) Decimal { + d.ensureInitialized() + d2.ensureInitialized() + + expInt64 := int64(d.exp) + int64(d2.exp) + if expInt64 > math.MaxInt32 || expInt64 < math.MinInt32 { + // NOTE(vadim): better to panic than give incorrect results, as + // Decimals are usually used for money + panic(fmt.Sprintf("exponent %v overflows an int32!", expInt64)) + } + + d3Value := new(big.Int).Mul(d.value, d2.value) + return Decimal{ + value: d3Value, + exp: int32(expInt64), + } +} + +// Shift shifts the decimal in base 10. +// It shifts left when shift is positive and right if shift is negative. +// In simpler terms, the given value for shift is added to the exponent +// of the decimal. +func (d Decimal) Shift(shift int32) Decimal { + d.ensureInitialized() + return Decimal{ + value: new(big.Int).Set(d.value), + exp: d.exp + shift, + } +} + +// Div returns d / d2. If it doesn't divide exactly, the result will have +// DivisionPrecision digits after the decimal point. +func (d Decimal) Div(d2 Decimal) Decimal { + return d.DivRound(d2, int32(DivisionPrecision)) +} + +// QuoRem does divsion with remainder +// d.QuoRem(d2,precision) returns quotient q and remainder r such that +// d = d2 * q + r, q an integer multiple of 10^(-precision) +// 0 <= r < abs(d2) * 10 ^(-precision) if d>=0 +// 0 >= r > -abs(d2) * 10 ^(-precision) if d<0 +// Note that precision<0 is allowed as input. +func (d Decimal) QuoRem(d2 Decimal, precision int32) (Decimal, Decimal) { + d.ensureInitialized() + d2.ensureInitialized() + if d2.value.Sign() == 0 { + panic("decimal division by 0") + } + scale := -precision + e := int64(d.exp - d2.exp - scale) + if e > math.MaxInt32 || e < math.MinInt32 { + panic("overflow in decimal QuoRem") + } + var aa, bb, expo big.Int + var scalerest int32 + // d = a 10^ea + // d2 = b 10^eb + if e < 0 { + aa = *d.value + expo.SetInt64(-e) + bb.Exp(tenInt, &expo, nil) + bb.Mul(d2.value, &bb) + scalerest = d.exp + // now aa = a + // bb = b 10^(scale + eb - ea) + } else { + expo.SetInt64(e) + aa.Exp(tenInt, &expo, nil) + aa.Mul(d.value, &aa) + bb = *d2.value + scalerest = scale + d2.exp + // now aa = a ^ (ea - eb - scale) + // bb = b + } + var q, r big.Int + q.QuoRem(&aa, &bb, &r) + dq := Decimal{value: &q, exp: scale} + dr := Decimal{value: &r, exp: scalerest} + return dq, dr +} + +// DivRound divides and rounds to a given precision +// i.e. to an integer multiple of 10^(-precision) +// for a positive quotient digit 5 is rounded up, away from 0 +// if the quotient is negative then digit 5 is rounded down, away from 0 +// Note that precision<0 is allowed as input. +func (d Decimal) DivRound(d2 Decimal, precision int32) Decimal { + // QuoRem already checks initialization + q, r := d.QuoRem(d2, precision) + // the actual rounding decision is based on comparing r*10^precision and d2/2 + // instead compare 2 r 10 ^precision and d2 + var rv2 big.Int + rv2.Abs(r.value) + rv2.Lsh(&rv2, 1) + // now rv2 = abs(r.value) * 2 + r2 := Decimal{value: &rv2, exp: r.exp + precision} + // r2 is now 2 * r * 10 ^ precision + var c = r2.Cmp(d2.Abs()) + + if c < 0 { + return q + } + + if d.value.Sign()*d2.value.Sign() < 0 { + return q.Sub(New(1, -precision)) + } + + return q.Add(New(1, -precision)) +} + +// Mod returns d % d2. +func (d Decimal) Mod(d2 Decimal) Decimal { + quo := d.Div(d2).Truncate(0) + return d.Sub(d2.Mul(quo)) +} + +// Pow returns d to the power d2 +func (d Decimal) Pow(d2 Decimal) Decimal { + var temp Decimal + if d2.IntPart() == 0 { + return NewFromFloat(1) + } + temp = d.Pow(d2.Div(NewFromFloat(2))) + if d2.IntPart()%2 == 0 { + return temp.Mul(temp) + } + if d2.IntPart() > 0 { + return temp.Mul(temp).Mul(d) + } + return temp.Mul(temp).Div(d) +} + +// Cmp compares the numbers represented by d and d2 and returns: +// +// -1 if d < d2 +// 0 if d == d2 +// +1 if d > d2 +// +func (d Decimal) Cmp(d2 Decimal) int { + d.ensureInitialized() + d2.ensureInitialized() + + if d.exp == d2.exp { + return d.value.Cmp(d2.value) + } + + baseExp := min(d.exp, d2.exp) + rd := d.rescale(baseExp) + rd2 := d2.rescale(baseExp) + + return rd.value.Cmp(rd2.value) +} + +// Equal returns whether the numbers represented by d and d2 are equal. +func (d Decimal) Equal(d2 Decimal) bool { + return d.Cmp(d2) == 0 +} + +// Equals is deprecated, please use Equal method instead +func (d Decimal) Equals(d2 Decimal) bool { + return d.Equal(d2) +} + +// GreaterThan (GT) returns true when d is greater than d2. +func (d Decimal) GreaterThan(d2 Decimal) bool { + return d.Cmp(d2) == 1 +} + +// GreaterThanOrEqual (GTE) returns true when d is greater than or equal to d2. +func (d Decimal) GreaterThanOrEqual(d2 Decimal) bool { + cmp := d.Cmp(d2) + return cmp == 1 || cmp == 0 +} + +// LessThan (LT) returns true when d is less than d2. +func (d Decimal) LessThan(d2 Decimal) bool { + return d.Cmp(d2) == -1 +} + +// LessThanOrEqual (LTE) returns true when d is less than or equal to d2. +func (d Decimal) LessThanOrEqual(d2 Decimal) bool { + cmp := d.Cmp(d2) + return cmp == -1 || cmp == 0 +} + +// Sign returns: +// +// -1 if d < 0 +// 0 if d == 0 +// +1 if d > 0 +// +func (d Decimal) Sign() int { + if d.value == nil { + return 0 + } + return d.value.Sign() +} + +// IsPositive return +// +// true if d > 0 +// false if d == 0 +// false if d < 0 +func (d Decimal) IsPositive() bool { + return d.Sign() == 1 +} + +// IsNegative return +// +// true if d < 0 +// false if d == 0 +// false if d > 0 +func (d Decimal) IsNegative() bool { + return d.Sign() == -1 +} + +// IsZero return +// +// true if d == 0 +// false if d > 0 +// false if d < 0 +func (d Decimal) IsZero() bool { + return d.Sign() == 0 +} + +// Exponent returns the exponent, or scale component of the decimal. +func (d Decimal) Exponent() int32 { + return d.exp +} + +// Coefficient returns the coefficient of the decimal. It is scaled by 10^Exponent() +func (d Decimal) Coefficient() *big.Int { + // we copy the coefficient so that mutating the result does not mutate the + // Decimal. + return big.NewInt(0).Set(d.value) +} + +// IntPart returns the integer component of the decimal. +func (d Decimal) IntPart() int64 { + scaledD := d.rescale(0) + return scaledD.value.Int64() +} + +// Rat returns a rational number representation of the decimal. +func (d Decimal) Rat() *big.Rat { + d.ensureInitialized() + if d.exp <= 0 { + // NOTE(vadim): must negate after casting to prevent int32 overflow + denom := new(big.Int).Exp(tenInt, big.NewInt(-int64(d.exp)), nil) + return new(big.Rat).SetFrac(d.value, denom) + } + + mul := new(big.Int).Exp(tenInt, big.NewInt(int64(d.exp)), nil) + num := new(big.Int).Mul(d.value, mul) + return new(big.Rat).SetFrac(num, oneInt) +} + +// Float64 returns the nearest float64 value for d and a bool indicating +// whether f represents d exactly. +// For more details, see the documentation for big.Rat.Float64 +func (d Decimal) Float64() (f float64, exact bool) { + return d.Rat().Float64() +} + +// String returns the string representation of the decimal +// with the fixed point. +// +// Example: +// +// d := New(-12345, -3) +// println(d.String()) +// +// Output: +// +// -12.345 +// +func (d Decimal) String() string { + return d.string(true) +} + +// StringFixed returns a rounded fixed-point string with places digits after +// the decimal point. +// +// Example: +// +// NewFromFloat(0).StringFixed(2) // output: "0.00" +// NewFromFloat(0).StringFixed(0) // output: "0" +// NewFromFloat(5.45).StringFixed(0) // output: "5" +// NewFromFloat(5.45).StringFixed(1) // output: "5.5" +// NewFromFloat(5.45).StringFixed(2) // output: "5.45" +// NewFromFloat(5.45).StringFixed(3) // output: "5.450" +// NewFromFloat(545).StringFixed(-1) // output: "550" +// +func (d Decimal) StringFixed(places int32) string { + rounded := d.Round(places) + return rounded.string(false) +} + +// StringFixedBank returns a banker rounded fixed-point string with places digits +// after the decimal point. +// +// Example: +// +// NewFromFloat(0).StringFixed(2) // output: "0.00" +// NewFromFloat(0).StringFixed(0) // output: "0" +// NewFromFloat(5.45).StringFixed(0) // output: "5" +// NewFromFloat(5.45).StringFixed(1) // output: "5.4" +// NewFromFloat(5.45).StringFixed(2) // output: "5.45" +// NewFromFloat(5.45).StringFixed(3) // output: "5.450" +// NewFromFloat(545).StringFixed(-1) // output: "550" +// +func (d Decimal) StringFixedBank(places int32) string { + rounded := d.RoundBank(places) + return rounded.string(false) +} + +// StringFixedCash returns a Swedish/Cash rounded fixed-point string. For +// more details see the documentation at function RoundCash. +func (d Decimal) StringFixedCash(interval uint8) string { + rounded := d.RoundCash(interval) + return rounded.string(false) +} + +// Round rounds the decimal to places decimal places. +// If places < 0, it will round the integer part to the nearest 10^(-places). +// +// Example: +// +// NewFromFloat(5.45).Round(1).String() // output: "5.5" +// NewFromFloat(545).Round(-1).String() // output: "550" +// +func (d Decimal) Round(places int32) Decimal { + // truncate to places + 1 + ret := d.rescale(-places - 1) + + // add sign(d) * 0.5 + if ret.value.Sign() < 0 { + ret.value.Sub(ret.value, fiveInt) + } else { + ret.value.Add(ret.value, fiveInt) + } + + // floor for positive numbers, ceil for negative numbers + _, m := ret.value.DivMod(ret.value, tenInt, new(big.Int)) + ret.exp++ + if ret.value.Sign() < 0 && m.Cmp(zeroInt) != 0 { + ret.value.Add(ret.value, oneInt) + } + + return ret +} + +// RoundBank rounds the decimal to places decimal places. +// If the final digit to round is equidistant from the nearest two integers the +// rounded value is taken as the even number +// +// If places < 0, it will round the integer part to the nearest 10^(-places). +// +// Examples: +// +// NewFromFloat(5.45).Round(1).String() // output: "5.4" +// NewFromFloat(545).Round(-1).String() // output: "540" +// NewFromFloat(5.46).Round(1).String() // output: "5.5" +// NewFromFloat(546).Round(-1).String() // output: "550" +// NewFromFloat(5.55).Round(1).String() // output: "5.6" +// NewFromFloat(555).Round(-1).String() // output: "560" +// +func (d Decimal) RoundBank(places int32) Decimal { + + round := d.Round(places) + remainder := d.Sub(round).Abs() + + half := New(5, -places-1) + if remainder.Cmp(half) == 0 && round.value.Bit(0) != 0 { + if round.value.Sign() < 0 { + round.value.Add(round.value, oneInt) + } else { + round.value.Sub(round.value, oneInt) + } + } + + return round +} + +// RoundCash aka Cash/Penny/öre rounding rounds decimal to a specific +// interval. The amount payable for a cash transaction is rounded to the nearest +// multiple of the minimum currency unit available. The following intervals are +// available: 5, 10, 15, 25, 50 and 100; any other number throws a panic. +// 5: 5 cent rounding 3.43 => 3.45 +// 10: 10 cent rounding 3.45 => 3.50 (5 gets rounded up) +// 15: 10 cent rounding 3.45 => 3.40 (5 gets rounded down) +// 25: 25 cent rounding 3.41 => 3.50 +// 50: 50 cent rounding 3.75 => 4.00 +// 100: 100 cent rounding 3.50 => 4.00 +// For more details: https://en.wikipedia.org/wiki/Cash_rounding +func (d Decimal) RoundCash(interval uint8) Decimal { + var iVal *big.Int + switch interval { + case 5: + iVal = twentyInt + case 10: + iVal = tenInt + case 15: + if d.exp < 0 { + // TODO: optimize and reduce allocations + orgExp := d.exp + dOne := New(10^-int64(orgExp), orgExp) + d2 := d + d2.exp = 0 + if d2.Mod(fiveDec).Equal(Zero) { + d2.exp = orgExp + d2 = d2.Sub(dOne) + d = d2 + } + } + iVal = tenInt + case 25: + iVal = fourInt + case 50: + iVal = twoInt + case 100: + iVal = oneInt + default: + panic(fmt.Sprintf("Decimal does not support this Cash rounding interval `%d`. Supported: 5, 10, 15, 25, 50, 100", interval)) + } + dVal := Decimal{ + value: iVal, + } + // TODO: optimize those calculations to reduce the high allocations (~29 allocs). + return d.Mul(dVal).Round(0).Div(dVal).Truncate(2) +} + +// Floor returns the nearest integer value less than or equal to d. +func (d Decimal) Floor() Decimal { + d.ensureInitialized() + + if d.exp >= 0 { + return d + } + + exp := big.NewInt(10) + + // NOTE(vadim): must negate after casting to prevent int32 overflow + exp.Exp(exp, big.NewInt(-int64(d.exp)), nil) + + z := new(big.Int).Div(d.value, exp) + return Decimal{value: z, exp: 0} +} + +// Ceil returns the nearest integer value greater than or equal to d. +func (d Decimal) Ceil() Decimal { + d.ensureInitialized() + + if d.exp >= 0 { + return d + } + + exp := big.NewInt(10) + + // NOTE(vadim): must negate after casting to prevent int32 overflow + exp.Exp(exp, big.NewInt(-int64(d.exp)), nil) + + z, m := new(big.Int).DivMod(d.value, exp, new(big.Int)) + if m.Cmp(zeroInt) != 0 { + z.Add(z, oneInt) + } + return Decimal{value: z, exp: 0} +} + +// Truncate truncates off digits from the number, without rounding. +// +// NOTE: precision is the last digit that will not be truncated (must be >= 0). +// +// Example: +// +// decimal.NewFromString("123.456").Truncate(2).String() // "123.45" +// +func (d Decimal) Truncate(precision int32) Decimal { + d.ensureInitialized() + if precision >= 0 && -precision > d.exp { + return d.rescale(-precision) + } + return d +} + +// UnmarshalJSON implements the json.Unmarshaler interface. +func (d *Decimal) UnmarshalJSON(decimalBytes []byte) error { + if string(decimalBytes) == "null" { + return nil + } + + str, err := unquoteIfQuoted(decimalBytes) + if err != nil { + return fmt.Errorf("Error decoding string '%s': %s", decimalBytes, err) + } + + decimal, err := NewFromString(str) + *d = decimal + if err != nil { + return fmt.Errorf("Error decoding string '%s': %s", str, err) + } + return nil +} + +// MarshalJSON implements the json.Marshaler interface. +func (d Decimal) MarshalJSON() ([]byte, error) { + var str string + if MarshalJSONWithoutQuotes { + str = d.String() + } else { + str = "\"" + d.String() + "\"" + } + return []byte(str), nil +} + +// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. As a string representation +// is already used when encoding to text, this method stores that string as []byte +func (d *Decimal) UnmarshalBinary(data []byte) error { + // Extract the exponent + d.exp = int32(binary.BigEndian.Uint32(data[:4])) + + // Extract the value + d.value = new(big.Int) + return d.value.GobDecode(data[4:]) +} + +// MarshalBinary implements the encoding.BinaryMarshaler interface. +func (d Decimal) MarshalBinary() (data []byte, err error) { + // Write the exponent first since it's a fixed size + v1 := make([]byte, 4) + binary.BigEndian.PutUint32(v1, uint32(d.exp)) + + // Add the value + var v2 []byte + if v2, err = d.value.GobEncode(); err != nil { + return + } + + // Return the byte array + data = append(v1, v2...) + return +} + +// Scan implements the sql.Scanner interface for database deserialization. +func (d *Decimal) Scan(value interface{}) error { + // first try to see if the data is stored in database as a Numeric datatype + switch v := value.(type) { + + case float32: + *d = NewFromFloat(float64(v)) + return nil + + case float64: + // numeric in sqlite3 sends us float64 + *d = NewFromFloat(v) + return nil + + case int64: + // at least in sqlite3 when the value is 0 in db, the data is sent + // to us as an int64 instead of a float64 ... + *d = New(v, 0) + return nil + + default: + // default is trying to interpret value stored as string + str, err := unquoteIfQuoted(v) + if err != nil { + return err + } + *d, err = NewFromString(str) + return err + } +} + +// Value implements the driver.Valuer interface for database serialization. +func (d Decimal) Value() (driver.Value, error) { + return d.String(), nil +} + +// UnmarshalText implements the encoding.TextUnmarshaler interface for XML +// deserialization. +func (d *Decimal) UnmarshalText(text []byte) error { + str := string(text) + + dec, err := NewFromString(str) + *d = dec + if err != nil { + return fmt.Errorf("Error decoding string '%s': %s", str, err) + } + + return nil +} + +// MarshalText implements the encoding.TextMarshaler interface for XML +// serialization. +func (d Decimal) MarshalText() (text []byte, err error) { + return []byte(d.String()), nil +} + +// GobEncode implements the gob.GobEncoder interface for gob serialization. +func (d Decimal) GobEncode() ([]byte, error) { + return d.MarshalBinary() +} + +// GobDecode implements the gob.GobDecoder interface for gob serialization. +func (d *Decimal) GobDecode(data []byte) error { + return d.UnmarshalBinary(data) +} + +// StringScaled first scales the decimal then calls .String() on it. +// NOTE: buggy, unintuitive, and DEPRECATED! Use StringFixed instead. +func (d Decimal) StringScaled(exp int32) string { + return d.rescale(exp).String() +} + +func (d Decimal) string(trimTrailingZeros bool) string { + if d.exp >= 0 { + return d.rescale(0).value.String() + } + + abs := new(big.Int).Abs(d.value) + str := abs.String() + + var intPart, fractionalPart string + + // NOTE(vadim): this cast to int will cause bugs if d.exp == INT_MIN + // and you are on a 32-bit machine. Won't fix this super-edge case. + dExpInt := int(d.exp) + if len(str) > -dExpInt { + intPart = str[:len(str)+dExpInt] + fractionalPart = str[len(str)+dExpInt:] + } else { + intPart = "0" + + num0s := -dExpInt - len(str) + fractionalPart = strings.Repeat("0", num0s) + str + } + + if trimTrailingZeros { + i := len(fractionalPart) - 1 + for ; i >= 0; i-- { + if fractionalPart[i] != '0' { + break + } + } + fractionalPart = fractionalPart[:i+1] + } + + number := intPart + if len(fractionalPart) > 0 { + number += "." + fractionalPart + } + + if d.value.Sign() < 0 { + return "-" + number + } + + return number +} + +func (d *Decimal) ensureInitialized() { + if d.value == nil { + d.value = new(big.Int) + } +} + +// Min returns the smallest Decimal that was passed in the arguments. +// +// To call this function with an array, you must do: +// +// Min(arr[0], arr[1:]...) +// +// This makes it harder to accidentally call Min with 0 arguments. +func Min(first Decimal, rest ...Decimal) Decimal { + ans := first + for _, item := range rest { + if item.Cmp(ans) < 0 { + ans = item + } + } + return ans +} + +// Max returns the largest Decimal that was passed in the arguments. +// +// To call this function with an array, you must do: +// +// Max(arr[0], arr[1:]...) +// +// This makes it harder to accidentally call Max with 0 arguments. +func Max(first Decimal, rest ...Decimal) Decimal { + ans := first + for _, item := range rest { + if item.Cmp(ans) > 0 { + ans = item + } + } + return ans +} + +// Sum returns the combined total of the provided first and rest Decimals +func Sum(first Decimal, rest ...Decimal) Decimal { + total := first + for _, item := range rest { + total = total.Add(item) + } + + return total +} + +// Avg returns the average value of the provided first and rest Decimals +func Avg(first Decimal, rest ...Decimal) Decimal { + count := New(int64(len(rest)+1), 0) + sum := Sum(first, rest...) + return sum.Div(count) +} + +func min(x, y int32) int32 { + if x >= y { + return y + } + return x +} + +func unquoteIfQuoted(value interface{}) (string, error) { + var bytes []byte + + switch v := value.(type) { + case string: + bytes = []byte(v) + case []byte: + bytes = v + default: + return "", fmt.Errorf("Could not convert value '%+v' to byte array of type '%T'", + value, value) + } + + // If the amount is quoted, strip the quotes + if len(bytes) > 2 && bytes[0] == '"' && bytes[len(bytes)-1] == '"' { + bytes = bytes[1 : len(bytes)-1] + } + return string(bytes), nil +} + +// NullDecimal represents a nullable decimal with compatibility for +// scanning null values from the database. +type NullDecimal struct { + Decimal Decimal + Valid bool +} + +// Scan implements the sql.Scanner interface for database deserialization. +func (d *NullDecimal) Scan(value interface{}) error { + if value == nil { + d.Valid = false + return nil + } + d.Valid = true + return d.Decimal.Scan(value) +} + +// Value implements the driver.Valuer interface for database serialization. +func (d NullDecimal) Value() (driver.Value, error) { + if !d.Valid { + return nil, nil + } + return d.Decimal.Value() +} + +// UnmarshalJSON implements the json.Unmarshaler interface. +func (d *NullDecimal) UnmarshalJSON(decimalBytes []byte) error { + if string(decimalBytes) == "null" { + d.Valid = false + return nil + } + d.Valid = true + return d.Decimal.UnmarshalJSON(decimalBytes) +} + +// MarshalJSON implements the json.Marshaler interface. +func (d NullDecimal) MarshalJSON() ([]byte, error) { + if !d.Valid { + return []byte("null"), nil + } + return d.Decimal.MarshalJSON() +} + +// Trig functions + +// Atan returns the arctangent, in radians, of x. +func (x Decimal) Atan() Decimal { + if x.Equal(NewFromFloat(0.0)) { + return x + } + if x.GreaterThan(NewFromFloat(0.0)) { + return x.satan() + } + return x.Neg().satan().Neg() +} + +func (d Decimal) xatan() Decimal { + P0 := NewFromFloat(-8.750608600031904122785e-01) + P1 := NewFromFloat(-1.615753718733365076637e+01) + P2 := NewFromFloat(-7.500855792314704667340e+01) + P3 := NewFromFloat(-1.228866684490136173410e+02) + P4 := NewFromFloat(-6.485021904942025371773e+01) + Q0 := NewFromFloat(2.485846490142306297962e+01) + Q1 := NewFromFloat(1.650270098316988542046e+02) + Q2 := NewFromFloat(4.328810604912902668951e+02) + Q3 := NewFromFloat(4.853903996359136964868e+02) + Q4 := NewFromFloat(1.945506571482613964425e+02) + z := d.Mul(d) + b1 := P0.Mul(z).Add(P1).Mul(z).Add(P2).Mul(z).Add(P3).Mul(z).Add(P4).Mul(z) + b2 := z.Add(Q0).Mul(z).Add(Q1).Mul(z).Add(Q2).Mul(z).Add(Q3).Mul(z).Add(Q4) + z = b1.Div(b2) + z = d.Mul(z).Add(d) + return z +} + +// satan reduces its argument (known to be positive) +// to the range [0, 0.66] and calls xatan. +func (d Decimal) satan() Decimal { + Morebits := NewFromFloat(6.123233995736765886130e-17) // pi/2 = PIO2 + Morebits + Tan3pio8 := NewFromFloat(2.41421356237309504880) // tan(3*pi/8) + pi := NewFromFloat(3.14159265358979323846264338327950288419716939937510582097494459) + + if d.LessThanOrEqual(NewFromFloat(0.66)) { + return d.xatan() + } + if d.GreaterThan(Tan3pio8) { + return pi.Div(NewFromFloat(2.0)).Sub(NewFromFloat(1.0).Div(d).xatan()).Add(Morebits) + } + return pi.Div(NewFromFloat(4.0)).Add((d.Sub(NewFromFloat(1.0)).Div(d.Add(NewFromFloat(1.0)))).xatan()).Add(NewFromFloat(0.5).Mul(Morebits)) +} + +// sin coefficients + var _sin = [...]Decimal{ + NewFromFloat(1.58962301576546568060E-10), // 0x3de5d8fd1fd19ccd + NewFromFloat(-2.50507477628578072866E-8), // 0xbe5ae5e5a9291f5d + NewFromFloat(2.75573136213857245213E-6), // 0x3ec71de3567d48a1 + NewFromFloat(-1.98412698295895385996E-4), // 0xbf2a01a019bfdf03 + NewFromFloat(8.33333333332211858878E-3), // 0x3f8111111110f7d0 + NewFromFloat(-1.66666666666666307295E-1), // 0xbfc5555555555548 + } + +// Sin returns the sine of the radian argument x. + func (d Decimal) Sin() Decimal { + PI4A := NewFromFloat(7.85398125648498535156E-1) // 0x3fe921fb40000000, Pi/4 split into three parts + PI4B := NewFromFloat(3.77489470793079817668E-8) // 0x3e64442d00000000, + PI4C := NewFromFloat(2.69515142907905952645E-15) // 0x3ce8469898cc5170, + M4PI := NewFromFloat(1.273239544735162542821171882678754627704620361328125) // 4/pi + + if d.Equal(NewFromFloat(0.0)) { + return d + } + // make argument positive but save the sign + sign := false + if d.LessThan(NewFromFloat(0.0)) { + d = d.Neg() + sign = true + } + + j := d.Mul(M4PI).IntPart() // integer part of x/(Pi/4), as integer for tests on the phase angle + y := NewFromFloat(float64(j)) // integer part of x/(Pi/4), as float + + // map zeros to origin + if j&1 == 1 { + j++ + y = y.Add(NewFromFloat(1.0)) + } + j &= 7 // octant modulo 2Pi radians (360 degrees) + // reflect in x axis + if j > 3 { + sign = !sign + j -= 4 + } + z := d.Sub(y.Mul(PI4A)).Sub(y.Mul(PI4B)).Sub(y.Mul(PI4C)) // Extended precision modular arithmetic + zz := z.Mul(z) + + if j == 1 || j == 2 { + w := zz.Mul(zz).Mul(_cos[0].Mul(zz).Add(_cos[1]).Mul(zz).Add(_cos[2]).Mul(zz).Add(_cos[3]).Mul(zz).Add(_cos[4]).Mul(zz).Add(_cos[5])) + y = NewFromFloat(1.0).Sub(NewFromFloat(0.5).Mul(zz)).Add(w) + } else { + y = z.Add(z.Mul(zz).Mul(_sin[0].Mul(zz).Add(_sin[1]).Mul(zz).Add(_sin[2]).Mul(zz).Add(_sin[3]).Mul(zz).Add(_sin[4]).Mul(zz).Add(_sin[5]))) + } + if sign { + y = y.Neg() + } + return y + } + + // cos coefficients + var _cos = [...]Decimal{ + NewFromFloat(-1.13585365213876817300E-11), // 0xbda8fa49a0861a9b + NewFromFloat(2.08757008419747316778E-9), // 0x3e21ee9d7b4e3f05 + NewFromFloat(-2.75573141792967388112E-7), // 0xbe927e4f7eac4bc6 + NewFromFloat(2.48015872888517045348E-5), // 0x3efa01a019c844f5 + NewFromFloat(-1.38888888888730564116E-3), // 0xbf56c16c16c14f91 + NewFromFloat(4.16666666666665929218E-2), // 0x3fa555555555554b + } + + // Cos returns the cosine of the radian argument x. + func (d Decimal) Cos() Decimal { + + PI4A := NewFromFloat(7.85398125648498535156E-1) // 0x3fe921fb40000000, Pi/4 split into three parts + PI4B := NewFromFloat(3.77489470793079817668E-8) // 0x3e64442d00000000, + PI4C := NewFromFloat(2.69515142907905952645E-15) // 0x3ce8469898cc5170, + M4PI := NewFromFloat(1.273239544735162542821171882678754627704620361328125) // 4/pi + + // make argument positive + sign := false + if d.LessThan(NewFromFloat(0.0)) { + d = d.Neg() + } + + j := d.Mul(M4PI).IntPart() // integer part of x/(Pi/4), as integer for tests on the phase angle + y := NewFromFloat(float64(j)) // integer part of x/(Pi/4), as float + + // map zeros to origin + if j&1 == 1 { + j++ + y = y.Add(NewFromFloat(1.0)) + } + j &= 7 // octant modulo 2Pi radians (360 degrees) + // reflect in x axis + if j > 3 { + sign = !sign + j -= 4 + } + if j > 1 { + sign = !sign + } + + z := d.Sub(y.Mul(PI4A)).Sub(y.Mul(PI4B)).Sub(y.Mul(PI4C)) // Extended precision modular arithmetic + zz := z.Mul(z) + + if j == 1 || j == 2 { + y = z.Add(z.Mul(zz).Mul(_sin[0].Mul(zz).Add(_sin[1]).Mul(zz).Add(_sin[2]).Mul(zz).Add(_sin[3]).Mul(zz).Add(_sin[4]).Mul(zz).Add(_sin[5]))) + } else { + w := zz.Mul(zz).Mul(_cos[0].Mul(zz).Add(_cos[1]).Mul(zz).Add(_cos[2]).Mul(zz).Add(_cos[3]).Mul(zz).Add(_cos[4]).Mul(zz).Add(_cos[5])) + y = NewFromFloat(1.0).Sub(NewFromFloat(0.5).Mul(zz)).Add(w) + } + if sign { + y = y.Neg() + } + return y + } + + var _tanP = [...]Decimal{ + NewFromFloat(-1.30936939181383777646E+4), // 0xc0c992d8d24f3f38 + NewFromFloat(1.15351664838587416140E+6), // 0x413199eca5fc9ddd + NewFromFloat(-1.79565251976484877988E+7), // 0xc1711fead3299176 + } + var _tanQ = [...]Decimal{ + NewFromFloat(1.00000000000000000000E+0), + NewFromFloat(1.36812963470692954678E+4), //0x40cab8a5eeb36572 + NewFromFloat(-1.32089234440210967447E+6), //0xc13427bc582abc96 + NewFromFloat(2.50083801823357915839E+7), //0x4177d98fc2ead8ef + NewFromFloat(-5.38695755929454629881E+7), //0xc189afe03cbe5a31 + } + + // Tan returns the tangent of the radian argument x. + func (d Decimal) Tan() Decimal { + + PI4A := NewFromFloat(7.85398125648498535156E-1) // 0x3fe921fb40000000, Pi/4 split into three parts + PI4B := NewFromFloat(3.77489470793079817668E-8) // 0x3e64442d00000000, + PI4C := NewFromFloat(2.69515142907905952645E-15) // 0x3ce8469898cc5170, + M4PI := NewFromFloat(1.273239544735162542821171882678754627704620361328125) // 4/pi + + if d.Equal(NewFromFloat(0.0)) { + return d + } + + // make argument positive but save the sign + sign := false + if d.LessThan(NewFromFloat(0.0)) { + d = d.Neg() + sign = true + } + + j := d.Mul(M4PI).IntPart() // integer part of x/(Pi/4), as integer for tests on the phase angle + y := NewFromFloat(float64(j)) // integer part of x/(Pi/4), as float + + // map zeros to origin + if j&1 == 1 { + j++ + y = y.Add(NewFromFloat(1.0)) + } + + z := d.Sub(y.Mul(PI4A)).Sub(y.Mul(PI4B)).Sub(y.Mul(PI4C)) // Extended precision modular arithmetic + zz := z.Mul(z) + + if zz.GreaterThan(NewFromFloat(1e-14)) { + w := zz.Mul(_tanP[0].Mul(zz).Add(_tanP[1]).Mul(zz).Add(_tanP[2])) + x := zz.Add(_tanQ[1]).Mul(zz).Add(_tanQ[2]).Mul(zz).Add(_tanQ[3]).Mul(zz).Add(_tanQ[4]) + y = z.Add(z.Mul(w.Div(x))) + } else { + y = z + } + if j&2 == 2 { + y = NewFromFloat(-1.0).Div(y) + } + if sign { + y = y.Neg() + } + return y + } diff --git a/vendor/github.com/shopspring/decimal/rounding.go b/vendor/github.com/shopspring/decimal/rounding.go new file mode 100644 index 000000000..fdd74eaa8 --- /dev/null +++ b/vendor/github.com/shopspring/decimal/rounding.go @@ -0,0 +1,118 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Multiprecision decimal numbers. +// For floating-point formatting only; not general purpose. +// Only operations are assign and (binary) left/right shift. +// Can do binary floating point in multiprecision decimal precisely +// because 2 divides 10; cannot do decimal floating point +// in multiprecision binary precisely. +package decimal + +type floatInfo struct { + mantbits uint + expbits uint + bias int +} + +var float32info = floatInfo{23, 8, -127} +var float64info = floatInfo{52, 11, -1023} + +// roundShortest rounds d (= mant * 2^exp) to the shortest number of digits +// that will let the original floating point value be precisely reconstructed. +func roundShortest(d *decimal, mant uint64, exp int, flt *floatInfo) { + // If mantissa is zero, the number is zero; stop now. + if mant == 0 { + d.nd = 0 + return + } + + // Compute upper and lower such that any decimal number + // between upper and lower (possibly inclusive) + // will round to the original floating point number. + + // We may see at once that the number is already shortest. + // + // Suppose d is not denormal, so that 2^exp <= d < 10^dp. + // The closest shorter number is at least 10^(dp-nd) away. + // The lower/upper bounds computed below are at distance + // at most 2^(exp-mantbits). + // + // So the number is already shortest if 10^(dp-nd) > 2^(exp-mantbits), + // or equivalently log2(10)*(dp-nd) > exp-mantbits. + // It is true if 332/100*(dp-nd) >= exp-mantbits (log2(10) > 3.32). + minexp := flt.bias + 1 // minimum possible exponent + if exp > minexp && 332*(d.dp-d.nd) >= 100*(exp-int(flt.mantbits)) { + // The number is already shortest. + return + } + + // d = mant << (exp - mantbits) + // Next highest floating point number is mant+1 << exp-mantbits. + // Our upper bound is halfway between, mant*2+1 << exp-mantbits-1. + upper := new(decimal) + upper.Assign(mant*2 + 1) + upper.Shift(exp - int(flt.mantbits) - 1) + + // d = mant << (exp - mantbits) + // Next lowest floating point number is mant-1 << exp-mantbits, + // unless mant-1 drops the significant bit and exp is not the minimum exp, + // in which case the next lowest is mant*2-1 << exp-mantbits-1. + // Either way, call it mantlo << explo-mantbits. + // Our lower bound is halfway between, mantlo*2+1 << explo-mantbits-1. + var mantlo uint64 + var explo int + if mant > 1<<flt.mantbits || exp == minexp { + mantlo = mant - 1 + explo = exp + } else { + mantlo = mant*2 - 1 + explo = exp - 1 + } + lower := new(decimal) + lower.Assign(mantlo*2 + 1) + lower.Shift(explo - int(flt.mantbits) - 1) + + // The upper and lower bounds are possible outputs only if + // the original mantissa is even, so that IEEE round-to-even + // would round to the original mantissa and not the neighbors. + inclusive := mant%2 == 0 + + // Now we can figure out the minimum number of digits required. + // Walk along until d has distinguished itself from upper and lower. + for i := 0; i < d.nd; i++ { + l := byte('0') // lower digit + if i < lower.nd { + l = lower.d[i] + } + m := d.d[i] // middle digit + u := byte('0') // upper digit + if i < upper.nd { + u = upper.d[i] + } + + // Okay to round down (truncate) if lower has a different digit + // or if lower is inclusive and is exactly the result of rounding + // down (i.e., and we have reached the final digit of lower). + okdown := l != m || inclusive && i+1 == lower.nd + + // Okay to round up if upper has a different digit and either upper + // is inclusive or upper is bigger than the result of rounding up. + okup := m != u && (inclusive || m+1 < u || i+1 < upper.nd) + + // If it's okay to do either, then round to the nearest one. + // If it's okay to do only one, do it. + switch { + case okdown && okup: + d.Round(i + 1) + return + case okdown: + d.RoundDown(i + 1) + return + case okup: + d.RoundUp(i + 1) + return + } + } +} diff --git a/vendor/vendor.json b/vendor/vendor.json index 53474352d..0f11868bc 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -631,6 +631,12 @@ "revisionTime": "2016-06-18T19:32:21Z" }, { + "checksumSHA1": "dhcoAs6YFyvXiG55mx5WDxg+/4g=", + "path": "github.com/shopspring/decimal", + "revision": "cd690d0c9e2447b1ef2a129a6b7b49077da89b8e", + "revisionTime": "2018-07-09T20:31:17Z" + }, + { "checksumSHA1": "mGbTYZ8dHVTiPTTJu3ktp+84pPI=", "path": "github.com/stretchr/testify/assert", "revision": "890a5c3458b43e6104ff5da8dfa139d013d77544", |