forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f260b79
commit cecbc34
Showing
6 changed files
with
1,389 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
language: go | ||
|
||
install: ./install-deps-gen-code.sh | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env sh | ||
|
||
# install dependencies | ||
go get -u github.com/PuerkitoBio/pigeon | ||
go get golang.org/x/tools/cmd/goimports | ||
|
||
# generate source code for parser | ||
pigeon src/jsonlog/jsonlog.peg | goimports > src/jsonlog/parser.go | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2015 The OPA Authors. All rights reserved. | ||
// Use of this source code is governed by an Apache2 | ||
// license that can be found in the LICENSE file. | ||
|
||
{ | ||
|
||
package jsonlog | ||
|
||
// part of the initializer code block omitted for brevity | ||
|
||
var ops = map[string]func(int, int) int { | ||
"+": func(l, r int) int { | ||
return l + r | ||
}, | ||
"-": func(l, r int) int { | ||
return l - r | ||
}, | ||
"*": func(l, r int) int { | ||
return l * r | ||
}, | ||
"/": func(l, r int) int { | ||
return l / r | ||
}, | ||
} | ||
|
||
func toIfaceSlice(v interface{}) []interface{} { | ||
if v == nil { | ||
return nil | ||
} | ||
return v.([]interface{}) | ||
} | ||
|
||
func eval(first, rest interface{}) int { | ||
l := first.(int) | ||
restSl := toIfaceSlice(rest) | ||
for _, v := range restSl { | ||
restExpr := toIfaceSlice(v) | ||
r := restExpr[3].(int) | ||
op := restExpr[1].(string) | ||
l = ops[op](l, r) | ||
} | ||
return l | ||
} | ||
} | ||
|
||
|
||
Input <- expr:Expr EOF { | ||
return expr, nil | ||
} | ||
|
||
Expr <- _ first:Term rest:( _ AddOp _ Term )* _ { | ||
return eval(first, rest), nil | ||
} | ||
|
||
Term <- first:Factor rest:( _ MulOp _ Factor )* { | ||
return eval(first, rest), nil | ||
} | ||
|
||
Factor <- '(' expr:Expr ')' { | ||
return expr, nil | ||
} / integer:Integer { | ||
return integer, nil | ||
} | ||
|
||
AddOp <- ( '+' / '-' ) { | ||
return string(c.text), nil | ||
} | ||
|
||
MulOp <- ( '*' / '/' ) { | ||
return string(c.text), nil | ||
} | ||
|
||
Integer <- '-'? [0-9]+ { | ||
return strconv.Atoi(string(c.text)) | ||
} | ||
|
||
_ "whitespace" <- [ \n\t\r]* | ||
|
||
EOF <- !. | ||
|
Oops, something went wrong.