Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/svstanev/goexp
Recursive descent expression parser in Go
https://github.com/svstanev/goexp
expression expression-engine expression-evaluator expression-language golang parser recursive-descent-parser
Last synced: 30 days ago
JSON representation
Recursive descent expression parser in Go
- Host: GitHub
- URL: https://github.com/svstanev/goexp
- Owner: svstanev
- License: mit
- Created: 2019-02-14T14:00:24.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-11-02T13:08:02.000Z (about 5 years ago)
- Last Synced: 2024-08-03T17:19:51.915Z (4 months ago)
- Topics: expression, expression-engine, expression-evaluator, expression-language, golang, parser, recursive-descent-parser
- Language: Go
- Size: 25.4 KB
- Stars: 8
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- go-awesome - goexp - Recursive descent expression parser in Go (Open source library / Interpreter)
README
# goexp
Recursive descent expression parser in Go
## Installation
```
go get -u github.com/svstanev/goexp
```## Usage
```golang
package mainimport (
"fmt"
"math""github.com/svstanev/goexp"
"github.com/svstanev/goexp/types"
)func main() {
context := goexp.NewEvalContext(nil)
context.AddName("x", types.Integer(1))
context.AddName("y", types.Integer(3))
context.AddName("z", types.Integer(5))
context.AddMethod("max", func(args ...types.Integer) (interface{}, error) {
var res int64 = math.MinInt64
for _, value := range args {
n := int64(value)
if n > res {
res = n
}
}
return types.Integer(res), nil
})res, err := goexp.EvalString("max(x, y, z)", context)
if err != nil {
panic(err)
}
fmt.Println(res)
}
```## Expression language
### Syntax Grammar
```
expression -> logical_or
logical_or -> logical_and (("||") logical_and)*;
logical_and -> logical_not (("&&") logical_not)*;
logical_not -> "!"? equality;
equality -> comparison (("==" | "!=") comparison)*;
comparison -> addition (("<" | "<=" | ">" | ">=") addition)*;
addition -> multiplication (("+" | "-") multiplication)*;
multiplication -> negate (("*" | "/" | "%") negate)*;
power -> negate ("**" negate)*;
negate -> "-"? call;
call -> primary (("(" arguments? ")") | ("." IDENTIFIER))*;
primary -> "false" | "true" | "nil" | IDENTIFIER | NUMBER | STRING | "(" expression ")";arguments -> expression ("," expression)*;
```### Lexical Grammar
```
IDENTIFIER -> ALPHA (ALPHA | DIGIT)*;
NUMBER -> DIGIT* ("." DIGIT*)?;
STRING -> "'" * "'"
| '"' * '"';DIGIT -> '0'...'9'
ALPHA -> 'a'...'z'|'A'...'Z'|'_'
```