| 1 |
// Copyright 2021 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package parser |
| 16 |
|
| 17 |
import "fmt" |
| 18 |
|
| 19 |
type options struct { |
| 20 |
maxRecursionDepth int |
| 21 |
errorReportingLimit int |
| 22 |
errorRecoveryTokenLookaheadLimit int |
| 23 |
errorRecoveryLimit int |
| 24 |
expressionSizeCodePointLimit int |
| 25 |
macros map[string]Macro |
| 26 |
populateMacroCalls bool |
| 27 |
enableOptionalSyntax bool |
| 28 |
enableVariadicOperatorASTs bool |
| 29 |
enableIdentEscapeSyntax bool |
| 30 |
enableHiddenAccumulatorName bool |
| 31 |
} |
| 32 |
|
| 33 |
// Option configures the behavior of the parser. |
| 34 |
type Option func(*options) error |
| 35 |
|
| 36 |
// MaxRecursionDepth limits the maximum depth the parser will attempt to parse the expression before giving up. |
| 37 |
func MaxRecursionDepth(limit int) Option { |
| 38 |
return func(opts *options) error { |
| 39 |
if limit < -1 { |
| 40 |
return fmt.Errorf("max recursion depth must be greater than or equal to -1: %d", limit) |
| 41 |
} |
| 42 |
opts.maxRecursionDepth = limit |
| 43 |
return nil |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
// ErrorRecoveryLookaheadTokenLimit limits the number of lexer tokens that may be considered during error recovery. |
| 48 |
// |
| 49 |
// Error recovery often involves looking ahead in the input to determine if there's a point at which parsing may |
| 50 |
// successfully resume. In some pathological cases, the parser can look through quite a large set of input which |
| 51 |
// in turn generates a lot of back-tracking and performance degredation. |
| 52 |
// |
| 53 |
// The limit must be >= 1, and is recommended to be less than the default of 256. |
| 54 |
func ErrorRecoveryLookaheadTokenLimit(limit int) Option { |
| 55 |
return func(opts *options) error { |
| 56 |
if limit < 1 { |
| 57 |
return fmt.Errorf("error recovery lookahead token limit must be at least 1: %d", limit) |
| 58 |
} |
| 59 |
opts.errorRecoveryTokenLookaheadLimit = limit |
| 60 |
return nil |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// ErrorRecoveryLimit limits the number of attempts the parser will perform to recover from an error. |
| 65 |
func ErrorRecoveryLimit(limit int) Option { |
| 66 |
return func(opts *options) error { |
| 67 |
if limit < -1 { |
| 68 |
return fmt.Errorf("error recovery limit must be greater than or equal to -1: %d", limit) |
| 69 |
} |
| 70 |
opts.errorRecoveryLimit = limit |
| 71 |
return nil |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// ErrorReportingLimit limits the number of syntax error reports before terminating parsing. |
| 76 |
// |
| 77 |
// The limit must be at least 1. If unset, the limit will be 100. |
| 78 |
func ErrorReportingLimit(limit int) Option { |
| 79 |
return func(opts *options) error { |
| 80 |
if limit < 1 { |
| 81 |
return fmt.Errorf("error reporting limit must be at least 1: %d", limit) |
| 82 |
} |
| 83 |
opts.errorReportingLimit = limit |
| 84 |
return nil |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
// ExpressionSizeCodePointLimit is an option which limits the maximum code point count of an |
| 89 |
// expression. |
| 90 |
func ExpressionSizeCodePointLimit(expressionSizeCodePointLimit int) Option { |
| 91 |
return func(opts *options) error { |
| 92 |
if expressionSizeCodePointLimit < -1 { |
| 93 |
return fmt.Errorf("expression size code point limit must be greater than or equal to -1: %d", expressionSizeCodePointLimit) |
| 94 |
} |
| 95 |
opts.expressionSizeCodePointLimit = expressionSizeCodePointLimit |
| 96 |
return nil |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
// Macros adds the given macros to the parser. |
| 101 |
func Macros(macros ...Macro) Option { |
| 102 |
return func(opts *options) error { |
| 103 |
for _, m := range macros { |
| 104 |
if m != nil { |
| 105 |
if opts.macros == nil { |
| 106 |
opts.macros = make(map[string]Macro) |
| 107 |
} |
| 108 |
opts.macros[m.MacroKey()] = m |
| 109 |
} |
| 110 |
} |
| 111 |
return nil |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
// PopulateMacroCalls ensures that the original call signatures replaced by expanded macros |
| 116 |
// are preserved in the `SourceInfo` of parse result. |
| 117 |
func PopulateMacroCalls(populateMacroCalls bool) Option { |
| 118 |
return func(opts *options) error { |
| 119 |
opts.populateMacroCalls = populateMacroCalls |
| 120 |
return nil |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
// EnableOptionalSyntax enables syntax for optional field and index selection. |
| 125 |
func EnableOptionalSyntax(optionalSyntax bool) Option { |
| 126 |
return func(opts *options) error { |
| 127 |
opts.enableOptionalSyntax = optionalSyntax |
| 128 |
return nil |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
// EnableIdentEscapeSyntax enables backtick (`) escaped field identifiers. This |
| 133 |
// supports extended types of characters in identifiers, e.g. foo.`baz-bar`. |
| 134 |
func EnableIdentEscapeSyntax(enableIdentEscapeSyntax bool) Option { |
| 135 |
return func(opts *options) error { |
| 136 |
opts.enableIdentEscapeSyntax = enableIdentEscapeSyntax |
| 137 |
return nil |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
// EnableHiddenAccumulatorName uses an accumulator variable name that is not a |
| 142 |
// normally accessible identifier in source for comprehension macros. Compatibility notes: |
| 143 |
// with this option enabled, a parsed AST would be semantically the same as if disabled, but would |
| 144 |
// have different internal identifiers in any of the built-in comprehension sub-expressions. When |
| 145 |
// disabled, it is possible but almost certainly a logic error to access the accumulator variable. |
| 146 |
func EnableHiddenAccumulatorName(enabled bool) Option { |
| 147 |
return func(opts *options) error { |
| 148 |
opts.enableHiddenAccumulatorName = enabled |
| 149 |
return nil |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
// EnableVariadicOperatorASTs enables a compact representation of chained like-kind commutative |
| 154 |
// operators. e.g. `a || b || c || d` -> `call(op='||', args=[a, b, c, d])` |
| 155 |
// |
| 156 |
// The benefit of enabling variadic operators ASTs is a more compact representation deeply nested |
| 157 |
// logic graphs. |
| 158 |
func EnableVariadicOperatorASTs(varArgASTs bool) Option { |
| 159 |
return func(opts *options) error { |
| 160 |
opts.enableVariadicOperatorASTs = varArgASTs |
| 161 |
return nil |
| 162 |
} |
| 163 |
} |