[PATCH for Dlang support 4/4] examples: d: started using location tracking

Adela Vais <[email protected]>
Newsgroups gmane.comp.parsers.bison.patches
Message-ID <[email protected]>
* examples/d/calc/calc.y: Modified.
* examples/d/calc/calc.test: Check it.
---
 examples/d/calc/README.md   | 28 --------------------
 examples/d/calc/calc.test   | 22 ++++++++++++++-
 examples/d/calc/calc.y      | 53 ++++++++++++++++++++++++++++++++-----
 examples/d/calc/local.mk    |  4 +--
 examples/d/simple/README.md | 28 --------------------
 examples/d/simple/local.mk  |  6 ++---
 gnulib                      |  2 +-
 7 files changed, 74 insertions(+), 69 deletions(-)
 delete mode 100644 examples/d/calc/README.md
 delete mode 100644 examples/d/simple/README.md

diff --git a/examples/d/calc/README.md b/examples/d/calc/README.md
deleted file mode 100644
index fe05ef27..00000000
--- a/examples/d/calc/README.md
+++ /dev/null
@@ -1,28 +0,0 @@
-# Examples in D
-
-This directory contains examples of Bison grammar files in D.
-
-You can run `make` to compile these examples.  And `make clean` to tidy
-afterwards.
-
-## d/calc.y
-The usual calculator.
-
-<!---
-
-Local Variables:
-fill-column: 76
-ispell-dictionary: "american"
-End:
-
-Copyright (C) 2018-2020 Free Software Foundation, Inc.
-
-Permission is granted to copy, distribute and/or modify this document
-under the terms of the GNU Free Documentation License, Version 1.3 or
-any later version published by the Free Software Foundation; with no
-Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
-Texts.  A copy of the license is included in the "GNU Free
-Documentation License" file as part of this distribution.
-
-# LocalWords:  mfcalc calc parsers yy
---->
diff --git a/examples/d/calc/calc.test b/examples/d/calc/calc.test
index cf4f80a8..1650b220 100644
--- a/examples/d/calc/calc.test
+++ b/examples/d/calc/calc.test
@@ -23,4 +23,24 @@ run 0 7
 cat >input <<EOF
 1 + 2 * * 3
 EOF
-run 1 "err: syntax error, unexpected *, expecting + or - or ( or number"
+run 1 "err: 1.9: syntax error, unexpected *, expecting + or - or ( or number"
+
+cat >input <<EOF
+-8 * -8
+EOF
+run 0 64
+
+cat >input <<EOF
+1111 + 1111 2222
+EOF
+run 1 "err: 1.13-16: syntax error, unexpected number"
+
+cat >input <<EOF
+1 +
+EOF
+run 1 "err: 1.4-2.0: syntax error, unexpected end of line, expecting + or - or ( or number"
+
+cat >input <<EOF
+99 009
+EOF
+run 1 "err: 1.4-6: syntax error, unexpected number"
\ No newline at end of file
diff --git a/examples/d/calc/calc.y b/examples/d/calc/calc.y
index 15b4f7c7..7e6a64ca 100644
--- a/examples/d/calc/calc.y
+++ b/examples/d/calc/calc.y
@@ -22,6 +22,8 @@
 %define api.parser.class {Calc}
 %define parse.error verbose
 
+%locations
+
 %union {
   int ival;
 }
@@ -94,6 +96,9 @@ class CalcLexer(R) : Lexer
 
   this(R r) { input = r; }
 
+  YYPosition start;
+  YYPosition end;
+
   // Should be a local in main, shared with %parse-param.
   int exit_status = 0;
 
@@ -103,6 +108,12 @@ class CalcLexer(R) : Lexer
     stderr.writeln (s);
   }
 
+  void yyerror(YYLocation loc, string s)
+  {
+    stderr.write(loc.toString(), ": ");
+    yyerror(s);
+  }
+
   YYSemanticType semanticVal_;
 
   public final @property YYSemanticType semanticVal ()
@@ -116,24 +127,39 @@ class CalcLexer(R) : Lexer
 
     // Skip initial spaces
     while (!input.empty && input.front != '\n' && isWhite (input.front))
+    {
+      start = end;
+      end.column++;
       input.popFront;
+    }
 
     if (input.empty)
       return TokenKind.YYEOF;
 
     // Numbers.
     if (input.front.isNumber)
+    {
+      int lenChars = 0;
+      auto copy = input;
+      import std.conv : parse;
+      semanticVal_.ival = input.parse!int;
+      while (!input.empty && copy.front != input.front)
       {
-        import std.conv : parse;
-        semanticVal_.ival = input.parse!int;
-        return TokenKind.NUM;
+        lenChars++;
+        copy.popFront;
       }
+      start = end;
+      end.column += lenChars;
+      return TokenKind.NUM;
+    }
 
     // Individual characters
     auto ch = input.front;
     input.popFront;
+    start = end;
+    end.column++;
     switch (ch)
-      {
+    {
       case '=':  return TokenKind.EQ;
       case '+':  return TokenKind.PLUS;
       case '-':  return TokenKind.MINUS;
@@ -141,9 +167,24 @@ class CalcLexer(R) : Lexer
       case '/':  return TokenKind.SLASH;
       case '(':  return TokenKind.LPAR;
       case ')':  return TokenKind.RPAR;
-      case '\n': return TokenKind.EOL;
-      default: assert(0);
+      case '\n': 
+      {
+        end.line++;
+        end.column = 1;
+        return TokenKind.EOL;
       }
+      default: assert(0);
+    }
+  }
+
+  YYPosition startPos() const
+  {
+    return start;
+  }
+
+  YYPosition endPos() const
+  {
+    return end;
   }
 }
 
diff --git a/examples/d/calc/local.mk b/examples/d/calc/local.mk
index 4497e5a3..5159c085 100644
--- a/examples/d/calc/local.mk
+++ b/examples/d/calc/local.mk
@@ -32,5 +32,5 @@ EXTRA_DIST += %D%/calc.test
 %D%/calc: %D%/calc.d
 	$(AM_V_GEN) $(DC) $(DCFLAGS) -of$@ %D%/calc.d
 
-dist_d_DATA = %D%/calc.y %D%/Makefile %D%/README.md
-CLEANFILES += %D%/calc %D%/calc.[do]
+dist_d_DATA = %D%/calc.y %D%/Makefile
+CLEANFILES += %D%/calc %D%/calc.[do]
\ No newline at end of file
diff --git a/examples/d/simple/README.md b/examples/d/simple/README.md
deleted file mode 100644
index fe05ef27..00000000
--- a/examples/d/simple/README.md
+++ /dev/null
@@ -1,28 +0,0 @@
-# Examples in D
-
-This directory contains examples of Bison grammar files in D.
-
-You can run `make` to compile these examples.  And `make clean` to tidy
-afterwards.
-
-## d/calc.y
-The usual calculator.
-
-<!---
-
-Local Variables:
-fill-column: 76
-ispell-dictionary: "american"
-End:
-
-Copyright (C) 2018-2020 Free Software Foundation, Inc.
-
-Permission is granted to copy, distribute and/or modify this document
-under the terms of the GNU Free Documentation License, Version 1.3 or
-any later version published by the Free Software Foundation; with no
-Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
-Texts.  A copy of the license is included in the "GNU Free
-Documentation License" file as part of this distribution.
-
-# LocalWords:  mfcalc calc parsers yy
---->
diff --git a/examples/d/simple/local.mk b/examples/d/simple/local.mk
index 40f8f63a..5159c085 100644
--- a/examples/d/simple/local.mk
+++ b/examples/d/simple/local.mk
@@ -27,10 +27,10 @@ EXTRA_DIST += %D%/calc.test
 
 %D%/calc.d: %D%/calc.y $(dependencies)
 	$(AM_V_GEN)$(MKDIR_P) %D%
-	$(AM_V_at)$(BISON) $(srcdir)/%D%/calc.y -o $@
+	$(AM_V_at)$(BISON) -o $@ $(srcdir)/%D%/calc.y
 
 %D%/calc: %D%/calc.d
 	$(AM_V_GEN) $(DC) $(DCFLAGS) -of$@ %D%/calc.d
 
-dist_d_DATA = %D%/calc.y %D%/Makefile %D%/README.md
-CLEANFILES += %D%/calc %D%/calc.[do]
+dist_d_DATA = %D%/calc.y %D%/Makefile
+CLEANFILES += %D%/calc %D%/calc.[do]
\ No newline at end of file
diff --git a/gnulib b/gnulib
index 175e0bc7..37b6f129 160000
--- a/gnulib
+++ b/gnulib
@@ -1 +1 @@
-Subproject commit 175e0bc72808d564074c4adcc72aeadb74adfcc6
+Subproject commit 37b6f1294620be849f951dcb2f505207b435f88d
-- 
2.17.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.