Skip to content

Commit

Permalink
Require space between adjacent texts.
Browse files Browse the repository at this point in the history
  • Loading branch information
udhos committed Mar 1, 2024
1 parent 79c4069 commit be0063a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
22 changes: 21 additions & 1 deletion bulexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
stBlank lexState = iota
stNumber
stText
stAfterText
stIdent
stDot
stEOF
Expand Down Expand Up @@ -318,14 +319,33 @@ SCANNER:
}
switch {
case b == '\'':
l.state = stBlank
l.state = stAfterText
return l.consume(Token{Type: TkText})
default:
if errSave := l.buf.WriteByte(b); errSave != nil {
return Token{Type: TkError, Value: errSave.Error()}
}
}

case stAfterText:
switch errByte {
case io.EOF:
l.state = stEOF
break SCANNER
case nil:
default:
return Token{Type: TkError, Value: errByte.Error()}
}
switch {
case b == '\'':
return Token{Type: TkError, Value: "missing required space between texts"}
default:
if errUnread := l.reader.UnreadByte(); errUnread != nil {
return Token{Type: TkError, Value: errUnread.Error()}
}
l.state = stBlank
}

default:
return Token{Type: TkError, Value: fmt.Sprintf("unexpected state:%d", l.state)}
}
Expand Down
5 changes: 4 additions & 1 deletion bulexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ var testTable = []lexerTest{
{"variable", "myVar123", "IDENT(myVar123) EOF()"},
{"number", "123", "NUMBER(123) EOF()"},
{"number+ident", "123abc", "ERROR(letter-after-number)"},
{"text", "'short text'", "TEXT(short text) EOF()"},
{"text1", "'short text'", "TEXT(short text) EOF()"},
{"text2", "'a' 'b'", "TEXT(a) TEXT(b) EOF()"},
{"text3", " 'a' 'b' ", "TEXT(a) TEXT(b) EOF()"},
{"text4", " 'a''b' ", "TEXT(a) ERROR(missing required space between texts)"},
{"braces1", "()[]", "LPAR(() RPAR()) LSBKT([) RSBKT(]) EOF()"},
{"braces2", " ( ) [ ] ", "LPAR(() RPAR()) LSBKT([) RSBKT(]) EOF()"},
{"compare1", "<>!==<=>=", "LT(<) GT(>) NE(!=) EQ(=) LE(<=) GE(>=) EOF()"},
Expand Down
7 changes: 6 additions & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ var testTable = []parserTest{
{"list literal 4", "['one' 'two' 'three'] CONTAINS 'four'", `{}`, expectFalse},
{"list literal 5", "[1 2 3 4] CONTAINS Number(var1)", `{"var1":1}`, expectTrue},
{"list literal 6", "[1 2 3 4] CONTAINS Number(var1)", `{"var1":5}`, expectFalse},
{"list literal 7", "[ 'one' 'two' 'three' ] CONTAINS 'two'", `{}`, expectTrue},
{"list literal 8", "[ 'one' 'two' 'three' ] CONTAINS 'four'", `{}`, expectFalse},
{"list literal 9", "[ 'one''two' 'three' ] CONTAINS 'four'", `{}`, expectError},
{"list function empty", "List(var0) CONTAINS 2", `{"var0":[],"var1":1}`, expectFalse},
{"list function 1", "List('[1 2 3 4]') CONTAINS Number(var1)", `{"var1":1}`, expectError},
{"list function 2", "List(var0) CONTAINS Number(var1)", `{"var0":[1,2,3,4],"var1":1}`, expectTrue},
Expand Down Expand Up @@ -204,7 +207,9 @@ func TestSave(t *testing.T) {
table = append(table, tt)
}
buf, _ := json.Marshal(table)
ioutil.WriteFile(output, buf, 0777)
if err := os.WriteFile(output, buf, 0777); err != nil {
t.Errorf("write: %s: %v", output, err)
}
}

func TestParserFromFile(t *testing.T) {
Expand Down
18 changes: 18 additions & 0 deletions parser/tests/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,24 @@
"vars": "{\"var1\":5}",
"expected_result": "false"
},
{
"name": "list literal 7",
"rule": "[ 'one' 'two' 'three' ] CONTAINS 'two'",
"vars": "{}",
"expected_result": "true"
},
{
"name": "list literal 8",
"rule": "[ 'one' 'two' 'three' ] CONTAINS 'four'",
"vars": "{}",
"expected_result": "false"
},
{
"name": "list literal 9",
"rule": "[ 'one''two' 'three' ] CONTAINS 'four'",
"vars": "{}",
"expected_result": "error"
},
{
"name": "list function empty",
"rule": "List(var0) CONTAINS 2",
Expand Down

0 comments on commit be0063a

Please sign in to comment.