[Mlir-commits] [mlir] cb530ec - [mlir][Tutorial] Make parsing an empty file print a better error.

River Riddle llvmlistbot at llvm.org
Tue Mar 3 13:24:09 PST 2020


Author: Matthias Kramm
Date: 2020-03-03T13:21:05-08:00
New Revision: cb530ec8b841b8033032a8bf88d93f7d6df2dbe0

URL: https://github.com/llvm/llvm-project/commit/cb530ec8b841b8033032a8bf88d93f7d6df2dbe0
DIFF: https://github.com/llvm/llvm-project/commit/cb530ec8b841b8033032a8bf88d93f7d6df2dbe0.diff

LOG: [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

Added: 
    mlir/test/Examples/Toy/Ch1/empty.toy
    mlir/test/Examples/Toy/Ch2/empty.toy
    mlir/test/Examples/Toy/Ch3/empty.toy
    mlir/test/Examples/Toy/Ch4/empty.toy
    mlir/test/Examples/Toy/Ch5/empty.toy
    mlir/test/Examples/Toy/Ch6/empty.toy
    mlir/test/Examples/Toy/Ch7/empty.toy

Modified: 
    mlir/examples/toy/Ch1/include/toy/Parser.h
    mlir/examples/toy/Ch2/include/toy/Parser.h
    mlir/examples/toy/Ch3/include/toy/Parser.h
    mlir/examples/toy/Ch4/include/toy/Parser.h
    mlir/examples/toy/Ch5/include/toy/Parser.h
    mlir/examples/toy/Ch6/include/toy/Parser.h
    mlir/examples/toy/Ch7/include/toy/Parser.h

Removed: 
    


################################################################################
diff  --git a/mlir/examples/toy/Ch1/include/toy/Parser.h b/mlir/examples/toy/Ch1/include/toy/Parser.h
index e1634fca9d74..5a7b21265113 100644
--- a/mlir/examples/toy/Ch1/include/toy/Parser.h
+++ b/mlir/examples/toy/Ch1/include/toy/Parser.h
@@ -396,7 +396,11 @@ class Parser {
   /// 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 e1634fca9d74..5a7b21265113 100644
--- a/mlir/examples/toy/Ch2/include/toy/Parser.h
+++ b/mlir/examples/toy/Ch2/include/toy/Parser.h
@@ -396,7 +396,11 @@ class Parser {
   /// 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 e1634fca9d74..5a7b21265113 100644
--- a/mlir/examples/toy/Ch3/include/toy/Parser.h
+++ b/mlir/examples/toy/Ch3/include/toy/Parser.h
@@ -396,7 +396,11 @@ class Parser {
   /// 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 e1634fca9d74..5a7b21265113 100644
--- a/mlir/examples/toy/Ch4/include/toy/Parser.h
+++ b/mlir/examples/toy/Ch4/include/toy/Parser.h
@@ -396,7 +396,11 @@ class Parser {
   /// 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 e1634fca9d74..5a7b21265113 100644
--- a/mlir/examples/toy/Ch5/include/toy/Parser.h
+++ b/mlir/examples/toy/Ch5/include/toy/Parser.h
@@ -396,7 +396,11 @@ class Parser {
   /// 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 e1634fca9d74..5a7b21265113 100644
--- a/mlir/examples/toy/Ch6/include/toy/Parser.h
+++ b/mlir/examples/toy/Ch6/include/toy/Parser.h
@@ -396,7 +396,11 @@ class Parser {
   /// 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 3d9128178765..cb6598c5da2a 100644
--- a/mlir/examples/toy/Ch7/include/toy/Parser.h
+++ b/mlir/examples/toy/Ch7/include/toy/Parser.h
@@ -534,7 +534,11 @@ class Parser {
   /// 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 000000000000..1e1e83ad31d9
--- /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 000000000000..36d092e328c0
--- /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 000000000000..87baab2397e4
--- /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 000000000000..5ad37f7f96d7
--- /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 000000000000..d43f34e7b855
--- /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 000000000000..f221f38a1a5c
--- /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 000000000000..d9d4b8e8f4e5
--- /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


        


More information about the Mlir-commits mailing list