func TestParseClass(t *testing.T) { root, tokens := parseClass(tokenizer.Tokenize(` class Main { function void main() { return; } } `)) if root.Name != "class" { t.Errorf("expect node `<class>`, but got:\n%v", root.ToXML()) } node, i := root.Find(&Node{Name: "keyword", Value: "class"}) if !(node != nil && i == 0) { t.Errorf("expect node to have class keyword, but got:\n%v", root.ToXML()) } if node, _ := root.Find(&Node{Name: "subroutineDec"}); node == nil { t.Errorf("expect node to have subroutineDec, but got:\n%v", root.ToXML()) } if len(tokens) > 0 { t.Errorf("expect len(tokens) == 0, but actual: %v", len(tokens)) } }
func main() { if len(os.Args) < 2 { fmt.Fprintln(os.Stderr, "no files given") os.Exit(1) } parseMode := os.Args[1] == "parse" filename := os.Args[1] if parseMode { filename = os.Args[2] } data, err := ioutil.ReadFile(filename) if err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) } tokens := tokenizer.Tokenize(string(data)) tree := parser.Parse(tokens) if parseMode { fmt.Print(tree.ToXML()) } else { fmt.Print(compiler.Compile(tree)) } }
func TestParseLetStatementWithArrayIndex(t *testing.T) { node, tokens := parseLetStatement(tokenizer.Tokenize(`let a[2]="foo";`)) if node.Name != "letStatement" { t.Errorf("expect: letStatement, actual: %v", node.ToXML()) } if len(tokens) > 0 { t.Error("parse failed") } }
func TestParseVarDec(t *testing.T) { node, tokens := parseVarDec(tokenizer.Tokenize(`var int i, sum;`)) if node.Name != "varDec" { t.Errorf("expect Name:`varDec` but actual: %v", node.Name) } if len(tokens) > 0 { t.Error("parse fails") } }
func testParseTermSuccess(t *testing.T, source string) (*Node, []*tokenizer.Token) { root, tokens := parseTerm(tokenizer.Tokenize(source)) if len(tokens) > 0 { t.Errorf("`%s`: expect len(tokens) == 0, but actual: %v", source, len(tokens)) } if root.Name != "term" { t.Errorf("`%s`: expect node to be term, but actual: %s", source, root.ToXML()) } return root, tokens }
func TestParseClassWithField(t *testing.T) { root, tokens := parseClass(tokenizer.Tokenize(` class Main { field int x, y; static int size; function void main() { return; } } `)) if node, _ := root.Find(&Node{Name: "classVarDec"}); node == nil { t.Errorf("expect node to have classVarDec, but got:\n%v", root.ToXML()) } if len(tokens) > 0 { t.Errorf("expect len(tokens) == 0, but actual: %v", len(tokens)) } }
func TestParseClassWithMethod(t *testing.T) { root, tokens := parseClass(tokenizer.Tokenize(` class Foo { constructor Foo new() { return; } method boolean bar() { return true; } } `)) if node, _ := root.Find(&Node{Name: "subroutineDec"}); node == nil { t.Errorf("expect node to have classVarDec, but got:\n%v", root.ToXML()) } if len(tokens) > 0 { t.Errorf("expect len(tokens) == 0, but actual: %v", len(tokens)) } }
func testMainOutput(t *testing.T, jackFile string) { xmlFile := regexp.MustCompile(`\.jack$`).ReplaceAllString(jackFile, ".xml") name := strings.Split(filepath.Base(jackFile), ".")[0] code, err := ioutil.ReadFile(jackFile) if err != nil { t.Error(err) return } parserOutput := parser.Parse(tokenizer.Tokenize(string(code))).ToXML() file, _ := ioutil.TempFile("", "") file.Write([]byte(parserOutput)) output, err := exec.Command(toolPath, file.Name(), xmlFile).CombinedOutput() if err != nil { t.Errorf("%s: %s %v", name, output, err) } }
func parse(source string) *Node { node, _ := ParseStatements(tokenizer.Tokenize(source)) return node }