-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04c81bd
commit fdd36c8
Showing
9 changed files
with
1,228 additions
and
871 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package generator_test | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/goropikari/tlex/compiler/generator" | ||
) | ||
|
||
func TestParser(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
given string | ||
}{ | ||
{ | ||
name: "parser test", | ||
given: ` | ||
%{ | ||
import ( | ||
"fmt" | ||
"log" | ||
"bytes" | ||
) | ||
// generated lexer returned types are (int, error). | ||
const ( | ||
Keyword int = iota + 1 | ||
Type | ||
Identifier | ||
Digit | ||
Whitespace | ||
LParen | ||
RParen | ||
LBracket | ||
RBracket | ||
Operator | ||
Hiragana | ||
) | ||
%} | ||
%% | ||
if|for|while|func|return { return Keyword, nil } | ||
int|float64 { return Type, nil } | ||
[a-zA-Z][a-zA-Z0-9]* { return Identifier, nil } | ||
[1-9][0-9]* { return Digit, nil } | ||
[ \t\n\r]* { } | ||
"(" { return LParen, nil } | ||
")" { return RParen, nil } | ||
"{" { return LBracket, nil } | ||
"}" { return RBracket, nil } | ||
"+" { return Operator, nil } | ||
"-" { return Operator, nil } | ||
"*" { return Operator, nil } | ||
"/" { return Operator, nil } | ||
":=" { return Operator, nil } | ||
"==" { return Operator, nil } | ||
"!=" { return Operator, nil } | ||
[ぁ-ゔ]* { return Hiragana, nil } | ||
. {} | ||
%% | ||
user defined code | ||
`, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := bytes.NewBufferString(tt.given) | ||
p := generator.NewParser(bufio.NewReader(r)) | ||
def, rules, userCode := p.Parse() | ||
fmt.Println(def, rules, userCode) | ||
}) | ||
} | ||
} |
Oops, something went wrong.