| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 34 |
type Option func(*options) error |
| 35 |
|
| 36 |
|
| 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 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 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 |
|
| 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 |
|
| 76 |
|
| 77 |
|
| 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 |
|
| 89 |
|
| 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 |
|
| 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 |
|
| 116 |
|
| 117 |
func PopulateMacroCalls(populateMacroCalls bool) Option { |
| 118 |
return func(opts *options) error { |
| 119 |
opts.populateMacroCalls = populateMacroCalls |
| 120 |
return nil |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
func EnableOptionalSyntax(optionalSyntax bool) Option { |
| 126 |
return func(opts *options) error { |
| 127 |
opts.enableOptionalSyntax = optionalSyntax |
| 128 |
return nil |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
func EnableIdentEscapeSyntax(enableIdentEscapeSyntax bool) Option { |
| 135 |
return func(opts *options) error { |
| 136 |
opts.enableIdentEscapeSyntax = enableIdentEscapeSyntax |
| 137 |
return nil |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
func EnableHiddenAccumulatorName(enabled bool) Option { |
| 147 |
return func(opts *options) error { |
| 148 |
opts.enableHiddenAccumulatorName = enabled |
| 149 |
return nil |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
func EnableVariadicOperatorASTs(varArgASTs bool) Option { |
| 159 |
return func(opts *options) error { |
| 160 |
opts.enableVariadicOperatorASTs = varArgASTs |
| 161 |
return nil |
| 162 |
} |
| 163 |
} |