Added try ... catch.

This commit is contained in:
Marvin Blum
2015-10-25 13:51:53 +01:00
parent 9db5c2042d
commit c3c4ca6f7d
7 changed files with 39 additions and 6 deletions

View File

@@ -35,6 +35,8 @@ func parseBlock() {
parseFunction()
} else if accept("return") {
parseReturn()
} else if accept("try") {
parseTryCatch()
} else if accept("case") || accept("default") {
return
} else {
@@ -232,6 +234,20 @@ func parseReturn() {
appendOut(";", true)
}
func parseTryCatch() {
expect("try")
expect("{")
appendOut("try {", true)
parseBlock()
expect("}")
expect("catch")
expect("{")
appendOut("} catch {", true)
parseBlock()
expect("}")
appendOut("};", true)
}
// Everything that does not start with a keyword.
func parseStatement() {
// empty block

View File

@@ -101,6 +101,13 @@ func TestParserOperator(t *testing.T) {
equal(t, got, want)
}
func TestParserTryCatch(t *testing.T) {
got := getCompiled(t, "test/parser_try_catch.asl")
want := "try {\n} catch {\n};\n"
equal(t, got, want)
}
func getCompiled(t *testing.T, file string) string {
code, err := ioutil.ReadFile(file)

View File

@@ -41,7 +41,9 @@ var keywords = []string{
"false",
"case",
"default",
"return"}
"return",
"try",
"catch"}
var whitespace = []byte{' ', '\n', '\t'}
var identifier = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"

View File

@@ -19,7 +19,7 @@ func usage() {
func main() {
// read test file
code, _ := ioutil.ReadFile("in/complex.asl")
code, _ := ioutil.ReadFile("in/simple.asl")
token := asl.Tokenize(code)
out := asl.Parse(token, true)