Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix string literal support #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion internal/iantlr/alr/gengineLexer.interp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/iantlr/alr/gengine_base_listener.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

466 changes: 242 additions & 224 deletions internal/iantlr/alr/gengine_lexer.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/iantlr/alr/gengine_listener.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/iantlr/alr/gengine_parser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion internal/iantlr/gengine.g4
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ fragment X : [xX] ;
fragment Y : [yY] ;
fragment Z : [zZ] ;
fragment EXPONENT_NUM_PART : ('E'| 'e') '-'? DEC_DIGIT+;
fragment HEX_DIGIT : [0-9a-fA-F];
fragment OCTAL_DIGIT : [0-7];

fragment ESCAPED_VALUE
: '\\' ('u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
| 'U' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
| [abfnrtv\\'"]
| OCTAL_DIGIT OCTAL_DIGIT OCTAL_DIGIT
| 'x' HEX_DIGIT HEX_DIGIT)
;

NIL : N I L;
RULE : R U L E ;
Expand Down Expand Up @@ -187,7 +197,7 @@ RR_BRACE : '}';
LR_BRACKET : '(';
RR_BRACKET : ')';
DOT : '.' ;
DQUOTA_STRING : '"' ( '\\'. | '""' | ~('"'| '\\') )* '"';
DQUOTA_STRING : '"' (~["\\\r\n] | ESCAPED_VALUE)* '"';
DOTTEDNAME : SIMPLENAME DOT SIMPLENAME ;
DOUBLEDOTTEDNAME : SIMPLENAME DOT SIMPLENAME DOT SIMPLENAME;
REAL_LITERAL : (DEC_DIGIT+)? '.' DEC_DIGIT+
Expand Down
8 changes: 6 additions & 2 deletions internal/iparser/gengine_parser_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,17 @@ func (g *GengineParserListener) ExitStringLiteral(ctx *parser.StringLiteralConte
}
holder := g.Stack.Peek().(base.StringHolder)
text := ctx.GetText()
txt := strings.Trim(text, "\"")
txt, err := strconv.Unquote(text)
if err != nil {
g.AddError(fmt.Errorf("unquote string error:%v, %s", err, text))
return
}
if reflect.TypeOf(holder).String() == "*base.MapVar" {
if txt == "" {
g.AddError(errors.New("MAP key should not be null string"))
}
}
err := holder.AcceptString(txt)
err = holder.AcceptString(txt)
if err != nil {
g.AddError(err)
}
Expand Down
66 changes: 66 additions & 0 deletions test/string_literal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package test

import (
"fmt"
"testing"
"time"

"github.com/bilibili/gengine/builder"
"github.com/bilibili/gengine/context"
"github.com/bilibili/gengine/engine"
)

const ruleStringLiteral = `
rule "string_literal_test" "test"
begin


debug("abc")
debug("ab\"c")
debug("ab\\c")
debug("ab\nc")
debug("ab\tc")
debug("ab\u2333c")
debug("ab\\u2333c")
debug("Hello, 世界")
debug("Hello, \u4e16\u754c")
debug("\xe4\xb8\x96\xe7\x95\x8c")
debug("")
//debug("ab
//c")
name := "abc\"def"
debug(name)

end
`

func TestStringLiteral(t *testing.T) {

dataContext := context.NewDataContext()
dataContext.Add("debug", t.Log)

//init rule engine
ruleBuilder := builder.NewRuleBuilder(dataContext)

//resolve rules from string
start1 := time.Now().UnixNano()
err := ruleBuilder.BuildRuleFromString(ruleStringLiteral)
end1 := time.Now().UnixNano()

println(fmt.Sprintf("rules num:%d, load rules cost time:%d ns", len(ruleBuilder.Kc.RuleEntities), end1-start1))

if err != nil {
panic(err)
}
eng := engine.NewGengine()

start := time.Now().UnixNano()
// true: means when there are many rules, if one rule execute error,continue to execute rules after the occur error rule
err = eng.Execute(ruleBuilder, true)
end := time.Now().UnixNano()
if err != nil {
panic(err)
}
println(fmt.Sprintf("execute rule cost %d ns", end-start))

}