Skip to content

Commit

Permalink
Implement booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
osteele committed Jun 26, 2017
1 parent 6a06665 commit 6af4fca
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 90 deletions.
31 changes: 18 additions & 13 deletions chunk_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,24 @@ func TestChunkParser(t *testing.T) {
var chunkTests = []struct{ in, expected string }{
{"{{12}}", "12"},
{"{{x}}", "123"},
{"{%if true%}true{%endif%}", "true"},
{"{%if false%}false{%endif%}", ""},
{"{%if 0%}true{%endif%}", "true"},
{"{%if 1%}true{%endif%}", "true"},
{"{%if x%}true{%endif%}", "true"},
{"{%if y%}false{%endif%}", ""},
{"{%if 1%}true{%else%}false{%endif%}", "true"},
{"{%if y%}false{%else%}true{%endif%}", "true"},
{"{%if 1%}0{%elsif 1%}1{%else%}2{%endif%}", "0"},
{"{%if y%}0{%elsif 1%}1{%else%}2{%endif%}", "1"},
{"{%if y%}0{%elsif z%}1{%else%}2{%endif%}", "2"},
{"{%unless x%}false{%endif%}", ""},
{"{%unless y%}true{%endif%}", "true"},
{"{%unless x%}false{%else%}true{%endif%}", "true"},
{"{%unless y%}true{%else%}false{%endif%}", "true"},
{"{%unless y%}0{%elsif x%}1{%else%}2{%endif%}", "0"},
{"{%unless x%}0{%elsif x%}1{%else%}2{%endif%}", "1"},
{"{%unless x%}0{%elsif y%}1{%else%}2{%endif%}", "2"},
{"{%if y%}true{%endif%}", ""},
{"{%if true%}true{%endif%}", "true"},
{"{%if false%}false{%endif%}", ""},
{"{%if true%}true{%else%}false{%endif%}", "true"},
{"{%if false%}false{%else%}true{%endif%}", "true"},
{"{%if true%}0{%elsif true%}1{%else%}2{%endif%}", "0"},
{"{%if false%}0{%elsif true%}1{%else%}2{%endif%}", "1"},
{"{%if false%}0{%elsif false%}1{%else%}2{%endif%}", "2"},
{"{%unless true%}false{%endif%}", ""},
{"{%unless false%}true{%endif%}", "true"},
{"{%unless true%}false{%else%}true{%endif%}", "true"},
{"{%unless false%}true{%else%}false{%endif%}", "true"},
{"{%unless false%}0{%elsif true%}1{%else%}2{%endif%}", "0"},
{"{%unless true%}0{%elsif true%}1{%else%}2{%endif%}", "1"},
{"{%unless true%}0{%elsif false%}1{%else%}2{%endif%}", "2"},
}
4 changes: 2 additions & 2 deletions expression_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
val func(Context) interface{}
}
%type <val> expr
%token <val> NUMBER IDENTIFIER
%token <val> LITERAL IDENTIFIER
%token <name> RELATION
%%
top: expr { yylex.(*lexer).val = $1 };

expr: NUMBER | IDENTIFIER;
expr: LITERAL | IDENTIFIER;
202 changes: 131 additions & 71 deletions scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,95 +9,117 @@ import "strconv"

//line scanner.go:11
var _expression_actions []byte = []byte{
0, 1, 0, 1, 1, 1, 2, 1, 3,
1, 4, 1, 5, 1, 6,
0, 1, 0, 1, 1, 1, 6, 1, 7,
1, 8, 1, 9, 1, 10, 1, 11,
2, 2, 3, 2, 2, 4, 2, 2,
5,
}

var _expression_key_offsets []byte = []byte{
0, 0, 1, 3, 21, 24, 27, 29,
30, 37, 45, 52, 60, 68, 76, 84,
92, 100, 107,
0, 0, 1, 3, 23, 26, 29, 31,
32, 39, 47, 55, 63, 71, 79, 87,
95, 103, 111, 119, 127, 135, 143, 151,
159,
}

var _expression_trans_keys []byte = []byte{
61, 48, 57, 32, 33, 45, 62, 95,
97, 99, 111, 9, 13, 48, 57, 60,
61, 65, 90, 98, 122, 32, 9, 13,
46, 48, 57, 48, 57, 61, 95, 48,
57, 65, 90, 97, 122, 95, 110, 48,
57, 65, 90, 97, 122, 95, 48, 57,
65, 90, 97, 122, 95, 111, 48, 57,
65, 90, 97, 122, 95, 110, 48, 57,
65, 90, 97, 122, 95, 116, 48, 57,
65, 90, 97, 122, 95, 97, 48, 57,
65, 90, 98, 122, 95, 105, 48, 57,
65, 90, 97, 122, 95, 110, 48, 57,
65, 90, 97, 122, 95, 48, 57, 65,
90, 97, 122, 95, 48, 57, 65, 90,
97, 122,
97, 99, 102, 111, 116, 9, 13, 48,
57, 60, 61, 65, 90, 98, 122, 32,
9, 13, 46, 48, 57, 48, 57, 61,
95, 48, 57, 65, 90, 97, 122, 95,
110, 48, 57, 65, 90, 97, 122, 95,
100, 48, 57, 65, 90, 97, 122, 95,
111, 48, 57, 65, 90, 97, 122, 95,
110, 48, 57, 65, 90, 97, 122, 95,
116, 48, 57, 65, 90, 97, 122, 95,
97, 48, 57, 65, 90, 98, 122, 95,
105, 48, 57, 65, 90, 97, 122, 95,
110, 48, 57, 65, 90, 97, 122, 95,
115, 48, 57, 65, 90, 97, 122, 95,
97, 48, 57, 65, 90, 98, 122, 95,
108, 48, 57, 65, 90, 97, 122, 95,
115, 48, 57, 65, 90, 97, 122, 95,
101, 48, 57, 65, 90, 97, 122, 95,
114, 48, 57, 65, 90, 97, 122, 95,
114, 48, 57, 65, 90, 97, 122, 95,
117, 48, 57, 65, 90, 97, 122,
}

var _expression_single_lengths []byte = []byte{
0, 1, 0, 8, 1, 1, 0, 1,
1, 2, 1, 2, 2, 2, 2, 2,
2, 1, 1,
0, 1, 0, 10, 1, 1, 0, 1,
1, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2,
2,
}

var _expression_range_lengths []byte = []byte{
0, 0, 1, 5, 1, 1, 1, 0,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3,
}

var _expression_index_offsets []byte = []byte{
0, 0, 2, 4, 18, 21, 24, 26,
28, 33, 39, 44, 50, 56, 62, 68,
74, 80, 85,
0, 0, 2, 4, 20, 23, 26, 28,
30, 35, 41, 47, 53, 59, 65, 71,
77, 83, 89, 95, 101, 107, 113, 119,
125,
}

var _expression_indicies []byte = []byte{
0, 1, 2, 1, 3, 4, 5, 6,
7, 8, 9, 10, 3, 2, 4, 7,
7, 1, 3, 3, 11, 13, 2, 12,
13, 12, 0, 14, 7, 7, 7, 7,
15, 7, 16, 7, 7, 7, 15, 7,
7, 7, 7, 15, 7, 17, 7, 7,
7, 15, 7, 18, 7, 7, 7, 15,
7, 19, 7, 7, 7, 15, 7, 20,
7, 7, 7, 15, 7, 21, 7, 7,
7, 15, 7, 22, 7, 7, 7, 15,
7, 7, 7, 7, 15, 7, 7, 7,
7, 15,
7, 8, 9, 10, 11, 12, 3, 2,
4, 7, 7, 1, 3, 3, 13, 15,
2, 14, 15, 14, 0, 16, 7, 7,
7, 7, 17, 7, 19, 7, 7, 7,
18, 7, 20, 7, 7, 7, 18, 7,
21, 7, 7, 7, 18, 7, 22, 7,
7, 7, 18, 7, 23, 7, 7, 7,
18, 7, 24, 7, 7, 7, 18, 7,
25, 7, 7, 7, 18, 7, 26, 7,
7, 7, 18, 7, 20, 7, 7, 7,
18, 7, 27, 7, 7, 7, 18, 7,
28, 7, 7, 7, 18, 7, 29, 7,
7, 7, 18, 7, 30, 7, 7, 7,
18, 7, 20, 7, 7, 7, 18, 7,
31, 7, 7, 7, 18, 7, 29, 7,
7, 7, 18,
}

var _expression_trans_targs []byte = []byte{
3, 0, 5, 4, 1, 2, 7, 8,
9, 11, 18, 3, 3, 6, 3, 3,
10, 12, 13, 14, 15, 16, 17,
9, 11, 18, 22, 23, 3, 3, 6,
3, 3, 3, 10, 8, 12, 13, 14,
15, 16, 17, 19, 20, 21, 8, 24,
}

var _expression_trans_actions []byte = []byte{
5, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 13, 9, 0, 11, 7,
0, 0, 0, 0, 0, 0, 0,
5, 0, 0, 0, 0, 0, 0, 23,
0, 0, 0, 0, 0, 13, 7, 0,
9, 15, 11, 0, 20, 0, 0, 0,
0, 0, 0, 0, 0, 0, 17, 0,
}

var _expression_to_state_actions []byte = []byte{
0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0,
}

var _expression_from_state_actions []byte = []byte{
0, 0, 0, 3, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0,
}

var _expression_eof_trans []byte = []byte{
0, 0, 0, 0, 12, 13, 13, 15,
16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16,
0, 0, 0, 0, 14, 15, 15, 17,
18, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19,
19,
}

const expression_start int = 3
Expand All @@ -123,7 +145,7 @@ func newLexer(data []byte) *lexer {
pe: len(data),
}

//line scanner.go:127
//line scanner.go:149
{
lex.cs = expression_start
lex.ts = 0
Expand All @@ -140,7 +162,7 @@ func (lex *lexer) Lex(out *yySymType) int {
tok := 0


//line scanner.go:144
//line scanner.go:166
{
var _klen int
var _trans int
Expand All @@ -163,7 +185,7 @@ _resume:
//line NONE:1
lex.ts = ( lex.p)

//line scanner.go:167
//line scanner.go:189
}
}

Expand Down Expand Up @@ -234,27 +256,29 @@ _eof_trans:
_acts++
switch _expression_actions[_acts-1] {
case 2:
//line scanner.rl:51
//line NONE:1
lex.te = ( lex.p)+1
{ tok = RELATION; out.name = string(lex.data[lex.ts:lex.te]); ( lex.p)++; goto _out
}

case 3:
//line scanner.rl:36
lex.te = ( lex.p)
( lex.p)--
{
tok = IDENTIFIER
name := string(lex.data[lex.ts:lex.te])
out.val = func(ctx Context) interface{} { return ctx.Variables[name] }
( lex.p)++; goto _out

}
lex.act = 2;
case 4:
//line scanner.rl:42
//line scanner.rl:56
lex.act = 4;
case 5:
//line scanner.rl:41
lex.act = 5;
case 6:
//line scanner.rl:56
lex.te = ( lex.p)+1
{ tok = RELATION; out.name = string(lex.data[lex.ts:lex.te]); ( lex.p)++; goto _out
}
case 7:
//line scanner.rl:47
lex.te = ( lex.p)
( lex.p)--
{
tok = NUMBER
tok = LITERAL
n, err := strconv.ParseFloat(string(lex.data[lex.ts:lex.te]), 64)
if err != nil {
panic(err)
Expand All @@ -263,18 +287,54 @@ _eof_trans:
( lex.p)++; goto _out

}
case 5:
//line scanner.rl:51
case 8:
//line scanner.rl:56
lex.te = ( lex.p)
( lex.p)--
{ tok = RELATION; out.name = string(lex.data[lex.ts:lex.te]); ( lex.p)++; goto _out
}
case 6:
//line scanner.rl:61
case 9:
//line scanner.rl:41
lex.te = ( lex.p)
( lex.p)--
{
tok = IDENTIFIER
name := string(lex.data[lex.ts:lex.te])
out.val = func(ctx Context) interface{} { return ctx.Variables[name] }
( lex.p)++; goto _out

}
case 10:
//line scanner.rl:67
lex.te = ( lex.p)
( lex.p)--

case 11:
//line NONE:1
switch lex.act {
case 2:
{( lex.p) = ( lex.te) - 1

//line scanner.go:278
tok = LITERAL
val := string(lex.data[lex.ts:lex.te]) == "true"
out.val = func(_ Context) interface{} { return val }
}
case 4:
{( lex.p) = ( lex.te) - 1
tok = RELATION; out.name = string(lex.data[lex.ts:lex.te]); ( lex.p)++; goto _out
}
case 5:
{( lex.p) = ( lex.te) - 1

tok = IDENTIFIER
name := string(lex.data[lex.ts:lex.te])
out.val = func(ctx Context) interface{} { return ctx.Variables[name] }
( lex.p)++; goto _out

}
}

//line scanner.go:338
}
}

Expand All @@ -288,7 +348,7 @@ _again:
//line NONE:1
lex.ts = 0

//line scanner.go:292
//line scanner.go:352
}
}

Expand All @@ -310,7 +370,7 @@ _again:
_out: {}
}

//line scanner.rl:65
//line scanner.rl:71


return tok
Expand Down
10 changes: 8 additions & 2 deletions scanner.rl
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,19 @@ func (lex *lexer) Lex(out *yySymType) int {
tok := 0

%%{
action Bool {
tok = LITERAL
val := string(lex.data[lex.ts:lex.te]) == "true"
out.val = func(_ Context) interface{} { return val }
}
action Ident {
tok = IDENTIFIER
name := string(lex.data[lex.ts:lex.te])
out.val = func(ctx Context) interface{} { return ctx.Variables[name] }
fbreak;
}
action Number {
tok = NUMBER
tok = LITERAL
n, err := strconv.ParseFloat(string(lex.data[lex.ts:lex.te]), 64)
if err != nil {
panic(err)
Expand All @@ -54,10 +59,11 @@ func (lex *lexer) Lex(out *yySymType) int {
number = '-'? (digit+ ('.' digit*)?) ;

main := |*
ident => Ident; #{ tok = IDENTIFIER; out.name = string(lex.data[lex.ts:lex.te]); fbreak; };
number => Number;
("true" | "false") => Bool;
("==" | "!=" | ">" | ">" | ">=" | "<=") => Relation;
("and" | "or" | "contains") => Relation;
ident => Ident;
space+;
*|;

Expand Down
Loading

0 comments on commit 6af4fca

Please sign in to comment.