Skip to content

Commit

Permalink
修改解析规则
Browse files Browse the repository at this point in the history
  • Loading branch information
Becivells committed Dec 24, 2021
1 parent 8361a6d commit b47e1e3
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 215 deletions.
5 changes: 3 additions & 2 deletions internal/fxparser/FOFA.g4
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ query
| propertyName=fofaKeyword op=EQ propertyValue=fofaValue #compareExp
| propertyName=fofaKeyword op=SEQ propertyValue=fofaValue #scompareExp
| propertyName=fofaKeyword op=NOT propertyValue=fofaValue #noCompareExp
| sgatom=fofaValue #sgExp
;

fofaKeyword
Expand Down Expand Up @@ -100,7 +101,7 @@ FOFA_KEY
STRING
: '"' (ESC | ~ ["\\])* '"'
// | ([a-zA-Z0-9.]|('\\' ["\\/bfnrt]))*
| ([a-zA-Z0-9.]|('\\' ["\\/bfnrt]))*
;

WS: [\t ]+ -> skip;
WS: [\t ]+ -> skip;
16 changes: 9 additions & 7 deletions internal/fxparser/fxquery_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fofax/internal/fxparser/stack"
"fofax/internal/printer"
"github.com/antlr/antlr4/runtime/Go/antlr"
"os"
"strings"
)

Expand Down Expand Up @@ -58,15 +57,15 @@ func (ql *FxQueryListener) ExitAndLogicalExp(c *parser.AndLogicalExpContext) {
ql.Stack.Push(fmt.Sprintf("%s&&%s", left, right))
}

func (ql *FxQueryListener) ExitSgExp(c *parser.SgExpContext) {
ql.Stack.Push(c.GetText())
}

func (ql *FxQueryListener) ExitOrLogicalExp(c *parser.OrLogicalExpContext) {
right := ql.Stack.Pop()
left := ql.Stack.Pop()
ql.Stack.Push(fmt.Sprintf("%s||%s", left, right))
}
func (ql *FxQueryListener) VisitErrorNode(node antlr.ErrorNode) {
fmt.Printf("error:%s", node.GetText())
os.Exit(1)
}

func parseTree(query string) (parser.IStartContext, error) {
errListener := NewFxErrorListener()
Expand Down Expand Up @@ -95,8 +94,11 @@ func PrintParserTree(query string) {
if errListener.errors > 0 {
printer.Fatal("found syntax errors in input")
}
fmt.Println(tree.GetText())
fmt.Println(tree.ToStringTree([]string{""}, fofaxParser))
fmt.Println("Source: ", query)
fmt.Println("Parse: ")
printer.Infof("Source: %s", query)
printer.Infof("Parse: %s", tree.GetText())
printer.Infof("Tree: %s", tree.ToStringTree([]string{""}, fofaxParser))
}

func Query(query string) string {
Expand Down
2 changes: 2 additions & 0 deletions internal/fxparser/fxquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ var successCase = []ps{
Source: `title="beijing"&&header="elastic"`,
Parse: `title="beijing"&&header="elastic"`,
},
}

var fofaTx = []ps{
{
Source: `banner`,
Parse: `"banner"`,
Expand Down
2 changes: 1 addition & 1 deletion internal/fxparser/parser/FOFA.interp
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ fofaValue


atn:
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 14, 52, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 30, 10, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 38, 10, 3, 12, 3, 14, 3, 41, 11, 3, 3, 4, 3, 4, 3, 4, 5, 4, 46, 10, 4, 3, 5, 3, 5, 5, 5, 50, 10, 5, 3, 5, 2, 3, 4, 6, 2, 4, 6, 8, 2, 2, 2, 54, 2, 10, 3, 2, 2, 2, 4, 29, 3, 2, 2, 2, 6, 42, 3, 2, 2, 2, 8, 49, 3, 2, 2, 2, 10, 11, 5, 4, 3, 2, 11, 3, 3, 2, 2, 2, 12, 13, 8, 3, 1, 2, 13, 14, 7, 10, 2, 2, 14, 15, 5, 4, 3, 2, 15, 16, 7, 11, 2, 2, 16, 30, 3, 2, 2, 2, 17, 18, 5, 6, 4, 2, 18, 19, 7, 8, 2, 2, 19, 20, 5, 8, 5, 2, 20, 30, 3, 2, 2, 2, 21, 22, 5, 6, 4, 2, 22, 23, 7, 9, 2, 2, 23, 24, 5, 8, 5, 2, 24, 30, 3, 2, 2, 2, 25, 26, 5, 6, 4, 2, 26, 27, 7, 7, 2, 2, 27, 28, 5, 8, 5, 2, 28, 30, 3, 2, 2, 2, 29, 12, 3, 2, 2, 2, 29, 17, 3, 2, 2, 2, 29, 21, 3, 2, 2, 2, 29, 25, 3, 2, 2, 2, 30, 39, 3, 2, 2, 2, 31, 32, 12, 7, 2, 2, 32, 33, 7, 5, 2, 2, 33, 38, 5, 4, 3, 8, 34, 35, 12, 6, 2, 2, 35, 36, 7, 6, 2, 2, 36, 38, 5, 4, 3, 7, 37, 31, 3, 2, 2, 2, 37, 34, 3, 2, 2, 2, 38, 41, 3, 2, 2, 2, 39, 37, 3, 2, 2, 2, 39, 40, 3, 2, 2, 2, 40, 5, 3, 2, 2, 2, 41, 39, 3, 2, 2, 2, 42, 45, 7, 12, 2, 2, 43, 44, 7, 3, 2, 2, 44, 46, 7, 12, 2, 2, 45, 43, 3, 2, 2, 2, 45, 46, 3, 2, 2, 2, 46, 7, 3, 2, 2, 2, 47, 50, 7, 4, 2, 2, 48, 50, 7, 13, 2, 2, 49, 47, 3, 2, 2, 2, 49, 48, 3, 2, 2, 2, 50, 9, 3, 2, 2, 2, 7, 29, 37, 39, 45, 49]
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 14, 53, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 3, 31, 10, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 3, 39, 10, 3, 12, 3, 14, 3, 42, 11, 3, 3, 4, 3, 4, 3, 4, 5, 4, 47, 10, 4, 3, 5, 3, 5, 5, 5, 51, 10, 5, 3, 5, 2, 3, 4, 6, 2, 4, 6, 8, 2, 2, 2, 56, 2, 10, 3, 2, 2, 2, 4, 30, 3, 2, 2, 2, 6, 43, 3, 2, 2, 2, 8, 50, 3, 2, 2, 2, 10, 11, 5, 4, 3, 2, 11, 3, 3, 2, 2, 2, 12, 13, 8, 3, 1, 2, 13, 14, 7, 10, 2, 2, 14, 15, 5, 4, 3, 2, 15, 16, 7, 11, 2, 2, 16, 31, 3, 2, 2, 2, 17, 18, 5, 6, 4, 2, 18, 19, 7, 8, 2, 2, 19, 20, 5, 8, 5, 2, 20, 31, 3, 2, 2, 2, 21, 22, 5, 6, 4, 2, 22, 23, 7, 9, 2, 2, 23, 24, 5, 8, 5, 2, 24, 31, 3, 2, 2, 2, 25, 26, 5, 6, 4, 2, 26, 27, 7, 7, 2, 2, 27, 28, 5, 8, 5, 2, 28, 31, 3, 2, 2, 2, 29, 31, 5, 8, 5, 2, 30, 12, 3, 2, 2, 2, 30, 17, 3, 2, 2, 2, 30, 21, 3, 2, 2, 2, 30, 25, 3, 2, 2, 2, 30, 29, 3, 2, 2, 2, 31, 40, 3, 2, 2, 2, 32, 33, 12, 8, 2, 2, 33, 34, 7, 5, 2, 2, 34, 39, 5, 4, 3, 9, 35, 36, 12, 7, 2, 2, 36, 37, 7, 6, 2, 2, 37, 39, 5, 4, 3, 8, 38, 32, 3, 2, 2, 2, 38, 35, 3, 2, 2, 2, 39, 42, 3, 2, 2, 2, 40, 38, 3, 2, 2, 2, 40, 41, 3, 2, 2, 2, 41, 5, 3, 2, 2, 2, 42, 40, 3, 2, 2, 2, 43, 46, 7, 12, 2, 2, 44, 45, 7, 3, 2, 2, 45, 47, 7, 12, 2, 2, 46, 44, 3, 2, 2, 2, 46, 47, 3, 2, 2, 2, 47, 7, 3, 2, 2, 2, 48, 51, 7, 4, 2, 2, 49, 51, 7, 13, 2, 2, 50, 48, 3, 2, 2, 2, 50, 49, 3, 2, 2, 2, 51, 9, 3, 2, 2, 2, 7, 30, 38, 40, 46, 50]
2 changes: 1 addition & 1 deletion internal/fxparser/parser/FOFALexer.interp

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions internal/fxparser/parser/fofa_base_listener.go

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

Loading

0 comments on commit b47e1e3

Please sign in to comment.