Skip to content

Commit

Permalink
Merge branch 'master' of github.com:nbareil/yara-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
nbareil committed Mar 22, 2018
2 parents 86ae925 + 0efc0e2 commit 5d21b99
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 35 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -35,7 +35,8 @@ $ y2j --help
Usage of y2j: y2j [options] file.yar
options:
-indent int
Set number of indent spaces (default 2)
-o string
JSON output file
```
Expand Down
2 changes: 1 addition & 1 deletion cmd/y2j/main.go
Expand Up @@ -44,7 +44,7 @@ func main() {

enc := json.NewEncoder(out)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
enc.SetIndent("", opts.Indent)
err = enc.Encode(&ruleset)
if err != nil {
perror(`Error writing JSON: %s`, err)
Expand Down
16 changes: 15 additions & 1 deletion cmd/y2j/opts.go
Expand Up @@ -3,20 +3,34 @@ package main
import (
"flag"
"os"
"strings"
)

type options struct {
Indent string
Infile string
Outfile string
}

func getopt() options {
var o options
var (
o options
indent int
)

flag.IntVar(&indent, "indent", 2, "Set number of indent spaces")
flag.StringVar(&o.Outfile, "o", "", "JSON output file")

flag.Parse()

// Set indent
var sb strings.Builder
for i := 0; i < indent; i++ {
sb.WriteRune(' ')
}
o.Indent = sb.String()

// The yara file is the only positional argument
if n := flag.NArg(); n != 1 {
perror("Expected 1 input file; found %d", n)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions data/data.go
Expand Up @@ -108,8 +108,8 @@ type Metas []Meta
// A Meta is a simple key/value pair. Val should be restricted to
// int, string, and bool.
type Meta struct {
Key string
Val interface{}
Key string `json:"key"`
Val interface{} `json:"val"`
}

// Strings are slices of String. No two String structs may have the same
Expand Down
35 changes: 16 additions & 19 deletions grammar/lexer.go
Expand Up @@ -1746,7 +1746,7 @@ case 62:

//line grammar/lexer.l:304
{
/* syntax_error("unterminated string"); */
panic(fmt.Errorf("unterminated string"))
}
case 63:
/* rule 63 can match eol */
Expand All @@ -1765,7 +1765,7 @@ case 63:

//line grammar/lexer.l:309
{
/* syntax_error("illegal escape sequence"); */
panic(fmt.Errorf("illegal escape sequence"))
}
case 64:

Expand All @@ -1785,10 +1785,7 @@ case 64:
{
collectText = false

s := textBuilder.String()
// Trim in 2 steps, otherwise "abcs/is", -> "abc"
txt := strings.TrimRight(s, "is")
txt = strings.TrimRight(txt, "/")
txt := strings.TrimSuffix(textBuilder.String(), YYtext)

var mods data.StringModifiers

Expand Down Expand Up @@ -1832,7 +1829,7 @@ case 65:
}


//line grammar/lexer.l:352
//line grammar/lexer.l:349
{
}
case 66:
Expand All @@ -1849,7 +1846,7 @@ case 66:
}


//line grammar/lexer.l:356
//line grammar/lexer.l:353
{
}
case 67:
Expand All @@ -1866,7 +1863,7 @@ case 67:
}


//line grammar/lexer.l:360
//line grammar/lexer.l:357
{ }
case 68:
/* rule 68 can match eol */
Expand All @@ -1883,9 +1880,9 @@ case 68:
}


//line grammar/lexer.l:363
//line grammar/lexer.l:360
{
/* syntax_error("unterminated regular expression"); */
panic(fmt.Errorf("unterminated regular expression"))
}
case 69:

Expand All @@ -1901,7 +1898,7 @@ case 69:
}


//line grammar/lexer.l:368
//line grammar/lexer.l:365
{
collectText = true
textBuilder.Reset()
Expand All @@ -1921,7 +1918,7 @@ case 70:
}


//line grammar/lexer.l:375
//line grammar/lexer.l:372
{
collectText = true
textBuilder.Reset()
Expand All @@ -1942,7 +1939,7 @@ case 71:
}


//line grammar/lexer.l:382
//line grammar/lexer.l:379
{
// Match hex-digits with whitespace or comments. The latter are stripped
// out by hex_lexer.l
Expand All @@ -1969,7 +1966,7 @@ case 72:
}


//line grammar/lexer.l:395
//line grammar/lexer.l:392
/* skip whitespace */
case 73:

Expand All @@ -1985,7 +1982,7 @@ case 73:
}


//line grammar/lexer.l:397
//line grammar/lexer.l:394
{

r := int(yytext[0])
Expand All @@ -2011,9 +2008,9 @@ case 74:
}


//line grammar/lexer.l:409
//line grammar/lexer.l:406
yyout.Write(yytext)
//line grammar/lexer.go:2017
//line grammar/lexer.go:2014
// SKEL ----------------------------------------------------------------

case yyEndOfBuffer:
Expand Down Expand Up @@ -2474,7 +2471,7 @@ func YYmain(filenames ...string) (interface{}, error) {
}

// END OF SKELL --------------------------------------------------------
//line grammar/lexer.l:409
//line grammar/lexer.l:406



17 changes: 6 additions & 11 deletions grammar/lexer.l
Expand Up @@ -302,22 +302,19 @@ u?int(8|16|32)(be)? {
<str>\n {
/* syntax_error("unterminated string"); */
panic(fmt.Errorf("unterminated string"))
}
<str>\\(.|\n) {
/* syntax_error("illegal escape sequence"); */
panic(fmt.Errorf("illegal escape sequence"))
}
<regexp>\/i?s? {
collectText = false
s := textBuilder.String()
// Trim in 2 steps, otherwise "abcs/is", -> "abc"
txt := strings.TrimRight(s, "is")
txt = strings.TrimRight(txt, "/")
txt := strings.TrimSuffix(textBuilder.String(), YYtext)
var mods data.StringModifiers
Expand All @@ -334,8 +331,7 @@ u?int(8|16|32)(be)? {
default:
// Should be impossible
err := fmt.Errorf("Invalid regex modifier: %c", c)
panic(err)
panic(fmt.Errorf("Invalid regex modifier: %c", c))
}
}
Expand All @@ -361,7 +357,7 @@ u?int(8|16|32)(be)? {
<regexp>\n {
/* syntax_error("unterminated regular expression"); */
panic(fmt.Errorf("unterminated regular expression"))
}
Expand Down Expand Up @@ -402,8 +398,7 @@ u?int(8|16|32)(be)? {
return r
}
err := fmt.Errorf("non-ascii byte '%d'", r)
panic(err)
panic(fmt.Errorf("non-ascii byte '%d'", r))
}
%%

0 comments on commit 5d21b99

Please sign in to comment.