diff options
author | Matthias Kramm <kramm@google.com> | 2020-03-03 13:20:48 -0800 |
---|---|---|
committer | River Riddle <riddleriver@gmail.com> | 2020-03-03 13:21:05 -0800 |
commit | cb530ec8b841b8033032a8bf88d93f7d6df2dbe0 (patch) | |
tree | 95aa5d1bfeca85af40a4ff6455a7a01ac42a388c | |
parent | c10896682d0bb457b9d77fdd753ed9e7e6806db1 (diff) | |
download | llvm-cb530ec8b841b8033032a8bf88d93f7d6df2dbe0.zip llvm-cb530ec8b841b8033032a8bf88d93f7d6df2dbe0.tar.gz llvm-cb530ec8b841b8033032a8bf88d93f7d6df2dbe0.tar.bz2 |
[mlir][Tutorial] Make parsing an empty file print a better error.
Summary:
Previously, we would, for an empty file, print the somewhat confusing
Assertion `tok == curTok [...]' failed.
With this change, we now print
Parse error [...]: expected 'def' [...]
This only affects the parser from chapters 1-6, since the more advanced
chapter 7 parser is actually able to generate an empty module from an
empty file. Nonetheless, this commit also adds the additional check to
the chapter 7 parser, for consistency.
Differential Revision: https://reviews.llvm.org/D75534
-rw-r--r-- | mlir/examples/toy/Ch1/include/toy/Parser.h | 4 | ||||
-rw-r--r-- | mlir/examples/toy/Ch2/include/toy/Parser.h | 4 | ||||
-rw-r--r-- | mlir/examples/toy/Ch3/include/toy/Parser.h | 4 | ||||
-rw-r--r-- | mlir/examples/toy/Ch4/include/toy/Parser.h | 4 | ||||
-rw-r--r-- | mlir/examples/toy/Ch5/include/toy/Parser.h | 4 | ||||
-rw-r--r-- | mlir/examples/toy/Ch6/include/toy/Parser.h | 4 | ||||
-rw-r--r-- | mlir/examples/toy/Ch7/include/toy/Parser.h | 4 | ||||
-rw-r--r-- | mlir/test/Examples/Toy/Ch1/empty.toy | 3 | ||||
-rw-r--r-- | mlir/test/Examples/Toy/Ch2/empty.toy | 3 | ||||
-rw-r--r-- | mlir/test/Examples/Toy/Ch3/empty.toy | 3 | ||||
-rw-r--r-- | mlir/test/Examples/Toy/Ch4/empty.toy | 3 | ||||
-rw-r--r-- | mlir/test/Examples/Toy/Ch5/empty.toy | 3 | ||||
-rw-r--r-- | mlir/test/Examples/Toy/Ch6/empty.toy | 3 | ||||
-rw-r--r-- | mlir/test/Examples/Toy/Ch7/empty.toy | 4 |
14 files changed, 50 insertions, 0 deletions
diff --git a/mlir/examples/toy/Ch1/include/toy/Parser.h b/mlir/examples/toy/Ch1/include/toy/Parser.h index e1634fc..5a7b212 100644 --- a/mlir/examples/toy/Ch1/include/toy/Parser.h +++ b/mlir/examples/toy/Ch1/include/toy/Parser.h @@ -396,7 +396,11 @@ private: /// decl_list ::= identifier | identifier, decl_list std::unique_ptr<PrototypeAST> parsePrototype() { auto loc = lexer.getLastLocation(); + + if (lexer.getCurToken() != tok_def) + return parseError<PrototypeAST>("def", "in prototype"); lexer.consume(tok_def); + if (lexer.getCurToken() != tok_identifier) return parseError<PrototypeAST>("function name", "in prototype"); diff --git a/mlir/examples/toy/Ch2/include/toy/Parser.h b/mlir/examples/toy/Ch2/include/toy/Parser.h index e1634fc..5a7b212 100644 --- a/mlir/examples/toy/Ch2/include/toy/Parser.h +++ b/mlir/examples/toy/Ch2/include/toy/Parser.h @@ -396,7 +396,11 @@ private: /// decl_list ::= identifier | identifier, decl_list std::unique_ptr<PrototypeAST> parsePrototype() { auto loc = lexer.getLastLocation(); + + if (lexer.getCurToken() != tok_def) + return parseError<PrototypeAST>("def", "in prototype"); lexer.consume(tok_def); + if (lexer.getCurToken() != tok_identifier) return parseError<PrototypeAST>("function name", "in prototype"); diff --git a/mlir/examples/toy/Ch3/include/toy/Parser.h b/mlir/examples/toy/Ch3/include/toy/Parser.h index e1634fc..5a7b212 100644 --- a/mlir/examples/toy/Ch3/include/toy/Parser.h +++ b/mlir/examples/toy/Ch3/include/toy/Parser.h @@ -396,7 +396,11 @@ private: /// decl_list ::= identifier | identifier, decl_list std::unique_ptr<PrototypeAST> parsePrototype() { auto loc = lexer.getLastLocation(); + + if (lexer.getCurToken() != tok_def) + return parseError<PrototypeAST>("def", "in prototype"); lexer.consume(tok_def); + if (lexer.getCurToken() != tok_identifier) return parseError<PrototypeAST>("function name", "in prototype"); diff --git a/mlir/examples/toy/Ch4/include/toy/Parser.h b/mlir/examples/toy/Ch4/include/toy/Parser.h index e1634fc..5a7b212 100644 --- a/mlir/examples/toy/Ch4/include/toy/Parser.h +++ b/mlir/examples/toy/Ch4/include/toy/Parser.h @@ -396,7 +396,11 @@ private: /// decl_list ::= identifier | identifier, decl_list std::unique_ptr<PrototypeAST> parsePrototype() { auto loc = lexer.getLastLocation(); + + if (lexer.getCurToken() != tok_def) + return parseError<PrototypeAST>("def", "in prototype"); lexer.consume(tok_def); + if (lexer.getCurToken() != tok_identifier) return parseError<PrototypeAST>("function name", "in prototype"); diff --git a/mlir/examples/toy/Ch5/include/toy/Parser.h b/mlir/examples/toy/Ch5/include/toy/Parser.h index e1634fc..5a7b212 100644 --- a/mlir/examples/toy/Ch5/include/toy/Parser.h +++ b/mlir/examples/toy/Ch5/include/toy/Parser.h @@ -396,7 +396,11 @@ private: /// decl_list ::= identifier | identifier, decl_list std::unique_ptr<PrototypeAST> parsePrototype() { auto loc = lexer.getLastLocation(); + + if (lexer.getCurToken() != tok_def) + return parseError<PrototypeAST>("def", "in prototype"); lexer.consume(tok_def); + if (lexer.getCurToken() != tok_identifier) return parseError<PrototypeAST>("function name", "in prototype"); diff --git a/mlir/examples/toy/Ch6/include/toy/Parser.h b/mlir/examples/toy/Ch6/include/toy/Parser.h index e1634fc..5a7b212 100644 --- a/mlir/examples/toy/Ch6/include/toy/Parser.h +++ b/mlir/examples/toy/Ch6/include/toy/Parser.h @@ -396,7 +396,11 @@ private: /// decl_list ::= identifier | identifier, decl_list std::unique_ptr<PrototypeAST> parsePrototype() { auto loc = lexer.getLastLocation(); + + if (lexer.getCurToken() != tok_def) + return parseError<PrototypeAST>("def", "in prototype"); lexer.consume(tok_def); + if (lexer.getCurToken() != tok_identifier) return parseError<PrototypeAST>("function name", "in prototype"); diff --git a/mlir/examples/toy/Ch7/include/toy/Parser.h b/mlir/examples/toy/Ch7/include/toy/Parser.h index 3d91281..cb6598c 100644 --- a/mlir/examples/toy/Ch7/include/toy/Parser.h +++ b/mlir/examples/toy/Ch7/include/toy/Parser.h @@ -534,7 +534,11 @@ private: /// decl_list ::= identifier | identifier, decl_list std::unique_ptr<PrototypeAST> parsePrototype() { auto loc = lexer.getLastLocation(); + + if (lexer.getCurToken() != tok_def) + return parseError<PrototypeAST>("def", "in prototype"); lexer.consume(tok_def); + if (lexer.getCurToken() != tok_identifier) return parseError<PrototypeAST>("function name", "in prototype"); diff --git a/mlir/test/Examples/Toy/Ch1/empty.toy b/mlir/test/Examples/Toy/Ch1/empty.toy new file mode 100644 index 0000000..1e1e83a --- /dev/null +++ b/mlir/test/Examples/Toy/Ch1/empty.toy @@ -0,0 +1,3 @@ +# RUN: toyc-ch1 %s -emit=ast 2>&1 | FileCheck %s +# CHECK-NOT: Assert +# CHECK: Parse error diff --git a/mlir/test/Examples/Toy/Ch2/empty.toy b/mlir/test/Examples/Toy/Ch2/empty.toy new file mode 100644 index 0000000..36d092e --- /dev/null +++ b/mlir/test/Examples/Toy/Ch2/empty.toy @@ -0,0 +1,3 @@ +# RUN: toyc-ch2 %s -emit=ast 2>&1 | FileCheck %s +# CHECK-NOT: Assert +# CHECK: Parse error diff --git a/mlir/test/Examples/Toy/Ch3/empty.toy b/mlir/test/Examples/Toy/Ch3/empty.toy new file mode 100644 index 0000000..87baab2 --- /dev/null +++ b/mlir/test/Examples/Toy/Ch3/empty.toy @@ -0,0 +1,3 @@ +# RUN: toyc-ch3 %s -emit=ast 2>&1 | FileCheck %s +# CHECK-NOT: Assert +# CHECK: Parse error diff --git a/mlir/test/Examples/Toy/Ch4/empty.toy b/mlir/test/Examples/Toy/Ch4/empty.toy new file mode 100644 index 0000000..5ad37f7 --- /dev/null +++ b/mlir/test/Examples/Toy/Ch4/empty.toy @@ -0,0 +1,3 @@ +# RUN: toyc-ch4 %s -emit=ast 2>&1 | FileCheck %s +# CHECK-NOT: Assert +# CHECK: Parse error diff --git a/mlir/test/Examples/Toy/Ch5/empty.toy b/mlir/test/Examples/Toy/Ch5/empty.toy new file mode 100644 index 0000000..d43f34e --- /dev/null +++ b/mlir/test/Examples/Toy/Ch5/empty.toy @@ -0,0 +1,3 @@ +# RUN: toyc-ch5 %s -emit=ast 2>&1 | FileCheck %s +# CHECK-NOT: Assert +# CHECK: Parse error diff --git a/mlir/test/Examples/Toy/Ch6/empty.toy b/mlir/test/Examples/Toy/Ch6/empty.toy new file mode 100644 index 0000000..f221f38 --- /dev/null +++ b/mlir/test/Examples/Toy/Ch6/empty.toy @@ -0,0 +1,3 @@ +# RUN: toyc-ch6 %s -emit=ast 2>&1 | FileCheck %s +# CHECK-NOT: Assert +# CHECK: Parse error diff --git a/mlir/test/Examples/Toy/Ch7/empty.toy b/mlir/test/Examples/Toy/Ch7/empty.toy new file mode 100644 index 0000000..d9d4b8e --- /dev/null +++ b/mlir/test/Examples/Toy/Ch7/empty.toy @@ -0,0 +1,4 @@ +# RUN: toyc-ch7 %s -emit=ast 2>&1 | FileCheck %s +# CHECK-NOT: Assert +# CHECK-NOT: Parse error +# CHECK: Module |