Re: [PATCH for Dlang support 1/2] d: change the return value of yylex from TokenKind to YYParser.Symbol
Adela Vais <[email protected]> Fri, 18 Dec 2020 20:37:13 +0200
| Newsgroups | gmane.comp.parsers.bison.patches |
|---|---|
| Message-ID | <CAPk8xGfCtBCGfUPAPRdBmYxkQ_XrnioZc3ChoMYsC4XJKyS6Pg@mail.gmail.com> |
Hello! I made the suggested modifications. I have a question: given that the user will provide all the needed information through the return value, should semanticVal(), startPos() and endPos() still be part of the Lexer interface? În vin., 20 nov. 2020 la 20:18, Akim Demaille <[email protected]> a scris: > You should introduce type aliases for b4_yystype and YYLocation. > In the C++ parser, you have value_type and location_type which > are defined to whatever they are actually. The code is nicer > to read, with fewer occurrences of ugly YYnames. > > I named them Location and Value. Should I also change b4_location_type to the new Location alias throughout lalr1.d? I know that the user should not use yy names (and before these commits, the examples used YYLocation) but I don't know how much of the backend I should change. I changed the b4_yystype occurrences. I also created an alias for YYPosition, which was part of the user interface. > > + SymbolKind token() { return kind; } > > + ]b4_yystype[ semanticValue() { return value; }]b4_locations_if([[ > > Make this value instead of semanticValue. > Done. > > @@ -75,7 +75,7 @@ public interface Lexer > > * to the next token and prepares to return the semantic value > > * ]b4_locations_if([and beginning/ending positions ])[of the token. > > * @@return the token identifier corresponding to the next token. */ > > - TokenKind yylex (); > > + ]b4_parser_class[.Symbol yylex (); > > Can't you provide an alias to avoid the need for the full path? > Done, I called it simply 'Symbol'. > > @@ -411,7 +411,9 @@ b4_locations_if([, ref ]b4_location_type[ > yylocationp])[) > > yycdebugln (message); > > } > > } > > -]])[ > > +]]) > > +b4_symbol_type_define > > +[ > > I would prefer > > > ]])[ > > ]b4_symbol_type_define[ > > > > for consistency. Done. > > if (yychar == TokenKind.]b4_symbol(empty, id)[) > > {]b4_parse_trace_if([[ > > yycdebugln ("Reading a token");]])[ > > - yychar = yylex ();]b4_locations_if([[ > > - static if (yy_location_is_class) { > > - yylloc = new ]b4_location_type[(yylexer.startPos, > yylexer.endPos); > > - } else { > > - yylloc = ]b4_location_type[(yylexer.startPos, > yylexer.endPos); > > - }]]) > > - yylval = yylexer.semanticVal;[ > > + Symbol yysymbol = yylex(); > > + yychar = yysymbol.token(); > > Maybe you don't need yychar, but only need yytoken. You probably > can avoid dealing with the token-kinds here, and deal only with > the symbol kinds. > Done. > > > + yylval = yysymbol.semanticValue();]b4_locations_if([[ > > + yylloc = yysymbol.location();]])[ > > } > > > +@deftypemethod {Lexer} {YYParser.Symbol} yylex() > > +Return the next token. The return value is of type YYParser.Symbol, > > Use @code{YYParser.Symbol}. Done. @code for TokenKind too. Done. > + case '+': return Calc.Symbol(TokenKind.PLUS, new > YYLocation(startPos, endPos)); > > + case '-': return Calc.Symbol(TokenKind.MINUS, new > YYLocation(startPos, endPos)); > > + case '*': return Calc.Symbol(TokenKind.STAR, new > YYLocation(startPos, endPos)); > > + case '/': return Calc.Symbol(TokenKind.SLASH, new > YYLocation(startPos, endPos)); > > + case '(': return Calc.Symbol(TokenKind.LPAR, new > YYLocation(startPos, endPos)); > > + case ')': return Calc.Symbol(TokenKind.RPAR, new > YYLocation(startPos, endPos)); > > This is super verbose. Can't you factor some 'loc' variable and use it? > Done > In modern C++ there's a feature I like: the constructor can be > called implicitly. So for instance > > MyStruct foo() { return 42; } > > actually means > > MyStruct foo() { return MyStruct(42); } > > If D has something equivalent, you can probably simplify all this. > > I'm afraid there is no equivalent in D. I have to explicitly call the constructor. > > + case '+': return > YYParser.Symbol(TokenKind.]AT_TOKEN_PREFIX[PLUS]AT_LOCATION_IF([[, new > YYLocation(startPos, endPos)]])[); > > + case '-': return > YYParser.Symbol(TokenKind.]AT_TOKEN_PREFIX[MINUS]AT_LOCATION_IF([[, new > YYLocation(startPos, endPos)]])[); > > + case '*': return > YYParser.Symbol(TokenKind.]AT_TOKEN_PREFIX[STAR]AT_LOCATION_IF([[, new > YYLocation(startPos, endPos)]])[); > > + case '/': return > YYParser.Symbol(TokenKind.]AT_TOKEN_PREFIX[SLASH]AT_LOCATION_IF([[, new > YYLocation(startPos, endPos)]]) > > Same comments about verbosity and redundancy. > Done. Adela
0001-d-change-name-of-YYParser.Symbol-s-semanticValue-to-.patch
(application/octet-stream, 2.1 KB)
From 71963438ce8d8ea73dab209c7e97f1e65dd19660 Mon Sep 17 00:00:00 2001 From: Adela Vais <[email protected]> Date: Thu, 10 Dec 2020 22:36:24 +0200 Subject: [PATCH for Dlang support 1/9] d: change name of YYParser.Symbol's semanticValue() to value() Member value was renamed to value_ to avoid the name clash. * data/skeletons/d.m4: Change member names. * data/skeletons/lalr1.d: Adjust. --- data/skeletons/d.m4 | 6 +++--- data/skeletons/lalr1.d | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/data/skeletons/d.m4 b/data/skeletons/d.m4 index b84d4e94..5362fb8e 100644 --- a/data/skeletons/d.m4 +++ b/data/skeletons/d.m4 @@ -460,7 +460,7 @@ m4_define([b4_symbol_type_define], struct Symbol { private SymbolKind kind; - private ]b4_yystype[ value;]b4_locations_if([[ + private ]b4_yystype[ value_;]b4_locations_if([[ private YYLocation location_;]])[ this(TokenKind token]b4_locations_if([[, YYLocation loc]])[) { @@ -472,12 +472,12 @@ m4_define([b4_symbol_type_define], this(TokenKind token, typeof(mixin("YYSemanticType." ~ member)) val]b4_locations_if([[, YYLocation loc]])[) { kind = yytranslate_(token); - mixin("value." ~ member ~ " = val;");]b4_locations_if([ + mixin("value_." ~ member ~ " = val;");]b4_locations_if([ location_ = loc;])[ } } SymbolKind token() { return kind; } - ]b4_yystype[ semanticValue() { return value; }]b4_locations_if([[ + ]b4_yystype[ value() { return value_; }]b4_locations_if([[ YYLocation location() { return location_; }]])[ } ]]) diff --git a/data/skeletons/lalr1.d b/data/skeletons/lalr1.d index d126d499..cea08dca 100644 --- a/data/skeletons/lalr1.d +++ b/data/skeletons/lalr1.d @@ -493,7 +493,7 @@ m4_popdef([b4_at_dollar])])dnl yycdebugln ("Reading a token");]])[ Symbol yysymbol = yylex(); yychar = yysymbol.token(); - yylval = yysymbol.semanticValue();]b4_locations_if([[ + yylval = yysymbol.value();]b4_locations_if([[ yylloc = yysymbol.location();]])[ } -- 2.17.1
0003-d-m4-style-consistency-fixup-around-b4_symbol_type_d.patch
(application/octet-stream, 879 B)
From 04e54dd4ddcb85e2849cfd9c1d30b87ae2ddf186 Mon Sep 17 00:00:00 2001 From: Adela Vais <[email protected]> Date: Thu, 10 Dec 2020 22:53:51 +0200 Subject: [PATCH for Dlang support 3/9] d: m4 style consistency fixup around b4_symbol_type_define call * data/skeletons/lalr1.d: Here. --- data/skeletons/lalr1.d | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/data/skeletons/lalr1.d b/data/skeletons/lalr1.d index a3378bce..fe07f88d 100644 --- a/data/skeletons/lalr1.d +++ b/data/skeletons/lalr1.d @@ -409,9 +409,8 @@ b4_locations_if([, ref ]b4_location_type[ yylocationp])[) yycdebugln (message); } } -]]) -b4_symbol_type_define -[ +]])[ +]b4_symbol_type_define[ /** * Parse input from the scanner that was specified at object construction * time. Return whether the end of the input was reached successfully. -- 2.17.1
0002-d-create-alias-Symbol-for-YYParse.Symbol.patch
(application/octet-stream, 13.1 KB)
From f1f1eb9cc38c71e4fbe0f8f324b2bd9103b3dcdf Mon Sep 17 00:00:00 2001 From: Adela Vais <[email protected]> Date: Thu, 10 Dec 2020 22:49:28 +0200 Subject: [PATCH for Dlang support 2/9] d: create alias Symbol for YYParse.Symbol * data/skeletons/lalr1.d: Here. * doc/bison.texi: Document it. * examples/d/calc/calc.y, examples/d/simple/calc.y: Adjust. * tests/calc.at, tests/d.at, tests/scanner.at: Test it. --- data/skeletons/d.m4 | 8 ++++++++ data/skeletons/lalr1.d | 4 +++- doc/bison.texi | 4 ++-- examples/d/calc/calc.y | 20 ++++++++++---------- examples/d/simple/calc.y | 20 ++++++++++---------- tests/calc.at | 30 +++++++++++++++--------------- tests/d.at | 8 ++++---- tests/scanner.at | 18 +++++++++--------- 8 files changed, 61 insertions(+), 51 deletions(-) diff --git a/data/skeletons/d.m4 b/data/skeletons/d.m4 index 5362fb8e..ee0d85a1 100644 --- a/data/skeletons/d.m4 +++ b/data/skeletons/d.m4 @@ -447,6 +447,14 @@ m4_define([b4_var_decls], m4_define([b4_var_decl], [ protected $1;]) +# b4_public_types_declare +# ----------------------- +# Define the public types: token, semantic value, location, and so forth. +# Depending on %define token_lex, may be output in the header or source file. +m4_define([b4_public_types_declare], +[[ +alias Symbol = ]b4_parser_class[.Symbol; +]]) # b4_symbol_type_define # --------------------- diff --git a/data/skeletons/lalr1.d b/data/skeletons/lalr1.d index cea08dca..a3378bce 100644 --- a/data/skeletons/lalr1.d +++ b/data/skeletons/lalr1.d @@ -75,7 +75,7 @@ public interface Lexer * to the next token and prepares to return the semantic value * ]b4_locations_if([and beginning/ending positions ])[of the token. * @@return the token identifier corresponding to the next token. */ - ]b4_parser_class[.Symbol yylex (); + Symbol yylex (); /** * Entry point for error reporting. Emits an error @@ -95,6 +95,8 @@ public interface Lexer ]])[ } +]b4_public_types_declare[ + ]b4_locations_if([b4_position_type_if([[ static assert(__traits(compiles, (new ]b4_position_type[[1])[0]=(new ]b4_position_type[[1])[0]), diff --git a/doc/bison.texi b/doc/bison.texi index 959a4039..314f662a 100644 --- a/doc/bison.texi +++ b/doc/bison.texi @@ -14011,8 +14011,8 @@ This method is defined by the user to emit an error message. The first parameter is omitted if location tracking is not active. @end deftypemethod -@deftypemethod {Lexer} {YYParser.Symbol} yylex() -Return the next token. The return value is of type YYParser.Symbol, which +@deftypemethod {Lexer} {Symbol} yylex() +Return the next token. The return value is of type Symbol, which binds together the TokenKind, the semantic value and the location. @end deftypemethod diff --git a/examples/d/calc/calc.y b/examples/d/calc/calc.y index 24ec85d4..8bf55fd4 100644 --- a/examples/d/calc/calc.y +++ b/examples/d/calc/calc.y @@ -114,7 +114,7 @@ if (isInputRange!R && is(ElementType!R : dchar)) return semanticVal_; } - Calc.Symbol yylex() + Symbol yylex() { import std.uni : isWhite, isNumber; @@ -127,7 +127,7 @@ if (isInputRange!R && is(ElementType!R : dchar)) } if (input.empty) - return Calc.Symbol(TokenKind.YYEOF, YYLocation(startPos, endPos)); + return Symbol(TokenKind.YYEOF, YYLocation(startPos, endPos)); // Numbers. if (input.front.isNumber) @@ -143,7 +143,7 @@ if (isInputRange!R && is(ElementType!R : dchar)) } start = end; end.column += lenChars; - return Calc.Symbol(TokenKind.NUM, semanticVal_.ival, YYLocation(startPos, endPos)); + return Symbol(TokenKind.NUM, semanticVal_.ival, YYLocation(startPos, endPos)); } // Individual characters @@ -153,17 +153,17 @@ if (isInputRange!R && is(ElementType!R : dchar)) end.column++; switch (ch) { - case '+': return Calc.Symbol(TokenKind.PLUS, YYLocation(startPos, endPos)); - case '-': return Calc.Symbol(TokenKind.MINUS, YYLocation(startPos, endPos)); - case '*': return Calc.Symbol(TokenKind.STAR, YYLocation(startPos, endPos)); - case '/': return Calc.Symbol(TokenKind.SLASH, YYLocation(startPos, endPos)); - case '(': return Calc.Symbol(TokenKind.LPAR, YYLocation(startPos, endPos)); - case ')': return Calc.Symbol(TokenKind.RPAR, YYLocation(startPos, endPos)); + case '+': return Symbol(TokenKind.PLUS, YYLocation(startPos, endPos)); + case '-': return Symbol(TokenKind.MINUS, YYLocation(startPos, endPos)); + case '*': return Symbol(TokenKind.STAR, YYLocation(startPos, endPos)); + case '/': return Symbol(TokenKind.SLASH, YYLocation(startPos, endPos)); + case '(': return Symbol(TokenKind.LPAR, YYLocation(startPos, endPos)); + case ')': return Symbol(TokenKind.RPAR, YYLocation(startPos, endPos)); case '\n': { end.line++; end.column = 1; - return Calc.Symbol(TokenKind.EOL, YYLocation(startPos, endPos)); + return Symbol(TokenKind.EOL, YYLocation(startPos, endPos)); } default: assert(0); } diff --git a/examples/d/simple/calc.y b/examples/d/simple/calc.y index ff20673d..de8da2a4 100644 --- a/examples/d/simple/calc.y +++ b/examples/d/simple/calc.y @@ -109,7 +109,7 @@ if (isInputRange!R && is(ElementType!R : dchar)) return semanticVal_; } - Calc.Symbol yylex() + Symbol yylex() { import std.uni : isWhite, isNumber; @@ -118,13 +118,13 @@ if (isInputRange!R && is(ElementType!R : dchar)) input.popFront; if (input.empty) - return Calc.Symbol(TokenKind.YYEOF); + return Symbol(TokenKind.YYEOF); // Numbers. if (input.front.isNumber) { import std.conv : parse; - return Calc.Symbol(TokenKind.NUM, input.parse!int); + return Symbol(TokenKind.NUM, input.parse!int); } // Individual characters @@ -132,13 +132,13 @@ if (isInputRange!R && is(ElementType!R : dchar)) input.popFront; switch (ch) { - case '+': return Calc.Symbol(TokenKind.PLUS); - case '-': return Calc.Symbol(TokenKind.MINUS); - case '*': return Calc.Symbol(TokenKind.STAR); - case '/': return Calc.Symbol(TokenKind.SLASH); - case '(': return Calc.Symbol(TokenKind.LPAR); - case ')': return Calc.Symbol(TokenKind.RPAR); - case '\n': return Calc.Symbol(TokenKind.EOL); + case '+': return Symbol(TokenKind.PLUS); + case '-': return Symbol(TokenKind.MINUS); + case '*': return Symbol(TokenKind.STAR); + case '/': return Symbol(TokenKind.SLASH); + case '(': return Symbol(TokenKind.LPAR); + case ')': return Symbol(TokenKind.RPAR); + case '\n': return Symbol(TokenKind.EOL); default: assert(0); } } diff --git a/tests/calc.at b/tests/calc.at index c11ab5c3..1c51d58a 100644 --- a/tests/calc.at +++ b/tests/calc.at @@ -590,7 +590,7 @@ class CalcLexer(R) : Lexer return res; } - YYParser.Symbol yylex () + Symbol yylex () {]AT_LOCATION_IF([[ location.begin = location.end;]])[ @@ -606,13 +606,13 @@ class CalcLexer(R) : Lexer // EOF. if (input.empty) - return YYParser.Symbol(TokenKind.]AT_TOKEN_PREFIX[EOF]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); + return Symbol(TokenKind.]AT_TOKEN_PREFIX[EOF]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); // Numbers. if (input.front.isNumber) { semanticVal_.ival = parseInt; - return YYParser.Symbol(TokenKind.]AT_TOKEN_PREFIX[NUM, semanticVal_.ival]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); + return Symbol(TokenKind.]AT_TOKEN_PREFIX[NUM, semanticVal_.ival]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); } // Individual characters @@ -630,22 +630,22 @@ class CalcLexer(R) : Lexer if (c == '#') { stderr.writeln (]AT_LOCATION_IF([location, ": ", ])["syntax error: invalid character: '#'"); - return YYParser.Symbol(TokenKind.]AT_TOKEN_PREFIX[YYerror]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); + return Symbol(TokenKind.]AT_TOKEN_PREFIX[YYerror]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); } switch (c) { - case '+': return YYParser.Symbol(TokenKind.]AT_TOKEN_PREFIX[PLUS]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - case '-': return YYParser.Symbol(TokenKind.]AT_TOKEN_PREFIX[MINUS]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - case '*': return YYParser.Symbol(TokenKind.]AT_TOKEN_PREFIX[STAR]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - case '/': return YYParser.Symbol(TokenKind.]AT_TOKEN_PREFIX[SLASH]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - case '(': return YYParser.Symbol(TokenKind.]AT_TOKEN_PREFIX[LPAR]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - case ')': return YYParser.Symbol(TokenKind.]AT_TOKEN_PREFIX[RPAR]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - case '\n': return YYParser.Symbol(TokenKind.]AT_TOKEN_PREFIX[EOL]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - case '=': return YYParser.Symbol(TokenKind.]AT_TOKEN_PREFIX[EQUAL]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - case '^': return YYParser.Symbol(TokenKind.]AT_TOKEN_PREFIX[POW]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - case '!': return YYParser.Symbol(TokenKind.]AT_TOKEN_PREFIX[NOT]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - default: return YYParser.Symbol(TokenKind.]AT_TOKEN_PREFIX[YYUNDEF]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); + case '+': return Symbol(TokenKind.]AT_TOKEN_PREFIX[PLUS]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); + case '-': return Symbol(TokenKind.]AT_TOKEN_PREFIX[MINUS]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); + case '*': return Symbol(TokenKind.]AT_TOKEN_PREFIX[STAR]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); + case '/': return Symbol(TokenKind.]AT_TOKEN_PREFIX[SLASH]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); + case '(': return Symbol(TokenKind.]AT_TOKEN_PREFIX[LPAR]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); + case ')': return Symbol(TokenKind.]AT_TOKEN_PREFIX[RPAR]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); + case '\n': return Symbol(TokenKind.]AT_TOKEN_PREFIX[EOL]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); + case '=': return Symbol(TokenKind.]AT_TOKEN_PREFIX[EQUAL]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); + case '^': return Symbol(TokenKind.]AT_TOKEN_PREFIX[POW]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); + case '!': return Symbol(TokenKind.]AT_TOKEN_PREFIX[NOT]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); + default: return Symbol(TokenKind.]AT_TOKEN_PREFIX[YYUNDEF]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); } } } diff --git a/tests/d.at b/tests/d.at index 38268c4c..3e83a8af 100644 --- a/tests/d.at +++ b/tests/d.at @@ -81,7 +81,7 @@ class CalcLexer(R) : Lexer YYSemanticType semanticVal_; YYSemanticType semanticVal() @property { return semanticVal_; } - YYParser.Symbol yylex() + Symbol yylex() { $2 } @@ -143,16 +143,16 @@ AT_KEYWORDS([d]) AT_CHECK_D_MINIMAL_W_LEXER([ %define api.token.raw true -%union { int ival; }], [return YYParser.Symbol(TokenKind.END);]) +%union { int ival; }], [return Symbol(TokenKind.END);]) AT_CHECK_D_GREP([[ END = 3,]]) AT_CHECK_D_MINIMAL_W_LEXER([ %define api.token.raw false -%union { int ival; }], [return YYParser.Symbol(TokenKind.END);]) +%union { int ival; }], [return Symbol(TokenKind.END);]) AT_CHECK_D_GREP([[ END = 258,]]) AT_CHECK_D_MINIMAL_W_LEXER([ -%union { int ival; }], [return YYParser.Symbol(TokenKind.END);]) +%union { int ival; }], [return Symbol(TokenKind.END);]) AT_CHECK_D_GREP([[ END = 3,]]) AT_CLEANUP diff --git a/tests/scanner.at b/tests/scanner.at index f19607ff..d0d3f390 100644 --- a/tests/scanner.at +++ b/tests/scanner.at @@ -121,12 +121,12 @@ class YYLexer(R) : Lexer return semanticVal_; } - YYParser.Symbol yylex () + Symbol yylex () { import std.uni : isNumber; // Handle EOF. if (input.empty) - return YYParser.Symbol(TokenKind.END); + return Symbol(TokenKind.END); auto c = input.front; input.popFront; @@ -136,13 +136,13 @@ class YYLexer(R) : Lexer { case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': semanticVal_.val = c - '0'; - return YYParser.Symbol(TokenKind.NUM, semanticVal_.val); - case '+': return YYParser.Symbol(TokenKind.PLUS); - case '-': return YYParser.Symbol(TokenKind.MINUS); - case '*': return YYParser.Symbol(TokenKind.STAR); - case '/': return YYParser.Symbol(TokenKind.SLASH); - case '(': return YYParser.Symbol(TokenKind.LPAR); - case ')': return YYParser.Symbol(TokenKind.RPAR); + return Symbol(TokenKind.NUM, semanticVal_.val); + case '+': return Symbol(TokenKind.PLUS); + case '-': return Symbol(TokenKind.MINUS); + case '*': return Symbol(TokenKind.STAR); + case '/': return Symbol(TokenKind.SLASH); + case '(': return Symbol(TokenKind.LPAR); + case ')': return Symbol(TokenKind.RPAR); default: assert(0); } } -- 2.17.1
0005-d-reduce-verbosity-for-returning-the-location-from-y.patch
(application/octet-stream, 6.6 KB)
From 605cfa51de53a784c1a45d4b552cc282b159370c Mon Sep 17 00:00:00 2001 From: Adela Vais <[email protected]> Date: Thu, 10 Dec 2020 23:27:53 +0200 Subject: [PATCH for Dlang support 5/9] d: reduce verbosity for returning the location from yylex() * examples/d/calc/calc.y: Add location as member variable in the Lexer class and use it. * tests/calc.at: Use the defined location variable. --- examples/d/calc/calc.y | 35 ++++++++++++++++++----------------- tests/calc.at | 28 ++++++++++++++-------------- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/examples/d/calc/calc.y b/examples/d/calc/calc.y index 8bf55fd4..3a8bf4eb 100644 --- a/examples/d/calc/calc.y +++ b/examples/d/calc/calc.y @@ -97,6 +97,7 @@ if (isInputRange!R && is(ElementType!R : dchar)) YYPosition start; YYPosition end; + YYLocation location; // Should be a local in main, shared with %parse-param. int exit_status = 0; @@ -121,13 +122,13 @@ if (isInputRange!R && is(ElementType!R : dchar)) // Skip initial spaces while (!input.empty && input.front != '\n' && isWhite(input.front)) { - start = end; - end.column++; + location.begin = location.end; + location.end.column++; input.popFront; } if (input.empty) - return Symbol(TokenKind.YYEOF, YYLocation(startPos, endPos)); + return Symbol(TokenKind.YYEOF, location); // Numbers. if (input.front.isNumber) @@ -141,29 +142,29 @@ if (isInputRange!R && is(ElementType!R : dchar)) lenChars++; copy.popFront; } - start = end; - end.column += lenChars; - return Symbol(TokenKind.NUM, semanticVal_.ival, YYLocation(startPos, endPos)); + location.begin = location.end; + location.end.column += lenChars; + return Symbol(TokenKind.NUM, semanticVal_.ival, location); } // Individual characters auto ch = input.front; input.popFront; - start = end; - end.column++; + location.begin = location.end; + location.end.column++; switch (ch) { - case '+': return Symbol(TokenKind.PLUS, YYLocation(startPos, endPos)); - case '-': return Symbol(TokenKind.MINUS, YYLocation(startPos, endPos)); - case '*': return Symbol(TokenKind.STAR, YYLocation(startPos, endPos)); - case '/': return Symbol(TokenKind.SLASH, YYLocation(startPos, endPos)); - case '(': return Symbol(TokenKind.LPAR, YYLocation(startPos, endPos)); - case ')': return Symbol(TokenKind.RPAR, YYLocation(startPos, endPos)); + case '+': return Symbol(TokenKind.PLUS, location); + case '-': return Symbol(TokenKind.MINUS, location); + case '*': return Symbol(TokenKind.STAR, location); + case '/': return Symbol(TokenKind.SLASH, location); + case '(': return Symbol(TokenKind.LPAR, location); + case ')': return Symbol(TokenKind.RPAR, location); case '\n': { - end.line++; - end.column = 1; - return Symbol(TokenKind.EOL, YYLocation(startPos, endPos)); + location.end.line++; + location.end.column = 1; + return Symbol(TokenKind.EOL, location); } default: assert(0); } diff --git a/tests/calc.at b/tests/calc.at index 1c51d58a..9abf263d 100644 --- a/tests/calc.at +++ b/tests/calc.at @@ -606,13 +606,13 @@ class CalcLexer(R) : Lexer // EOF. if (input.empty) - return Symbol(TokenKind.]AT_TOKEN_PREFIX[EOF]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); + return Symbol(TokenKind.]AT_TOKEN_PREFIX[EOF]AT_LOCATION_IF([[, location]])[); // Numbers. if (input.front.isNumber) { semanticVal_.ival = parseInt; - return Symbol(TokenKind.]AT_TOKEN_PREFIX[NUM, semanticVal_.ival]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); + return Symbol(TokenKind.]AT_TOKEN_PREFIX[NUM, semanticVal_.ival]AT_LOCATION_IF([[, location]])[); } // Individual characters @@ -630,22 +630,22 @@ class CalcLexer(R) : Lexer if (c == '#') { stderr.writeln (]AT_LOCATION_IF([location, ": ", ])["syntax error: invalid character: '#'"); - return Symbol(TokenKind.]AT_TOKEN_PREFIX[YYerror]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); + return Symbol(TokenKind.]AT_TOKEN_PREFIX[YYerror]AT_LOCATION_IF([[, location]])[); } switch (c) { - case '+': return Symbol(TokenKind.]AT_TOKEN_PREFIX[PLUS]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - case '-': return Symbol(TokenKind.]AT_TOKEN_PREFIX[MINUS]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - case '*': return Symbol(TokenKind.]AT_TOKEN_PREFIX[STAR]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - case '/': return Symbol(TokenKind.]AT_TOKEN_PREFIX[SLASH]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - case '(': return Symbol(TokenKind.]AT_TOKEN_PREFIX[LPAR]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - case ')': return Symbol(TokenKind.]AT_TOKEN_PREFIX[RPAR]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - case '\n': return Symbol(TokenKind.]AT_TOKEN_PREFIX[EOL]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - case '=': return Symbol(TokenKind.]AT_TOKEN_PREFIX[EQUAL]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - case '^': return Symbol(TokenKind.]AT_TOKEN_PREFIX[POW]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - case '!': return Symbol(TokenKind.]AT_TOKEN_PREFIX[NOT]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); - default: return Symbol(TokenKind.]AT_TOKEN_PREFIX[YYUNDEF]AT_LOCATION_IF([[, YYLocation(startPos, endPos)]])[); + case '+': return Symbol(TokenKind.]AT_TOKEN_PREFIX[PLUS]AT_LOCATION_IF([[, location]])[); + case '-': return Symbol(TokenKind.]AT_TOKEN_PREFIX[MINUS]AT_LOCATION_IF([[, location]])[); + case '*': return Symbol(TokenKind.]AT_TOKEN_PREFIX[STAR]AT_LOCATION_IF([[, location]])[); + case '/': return Symbol(TokenKind.]AT_TOKEN_PREFIX[SLASH]AT_LOCATION_IF([[, location]])[); + case '(': return Symbol(TokenKind.]AT_TOKEN_PREFIX[LPAR]AT_LOCATION_IF([[, location]])[); + case ')': return Symbol(TokenKind.]AT_TOKEN_PREFIX[RPAR]AT_LOCATION_IF([[, location]])[); + case '\n': return Symbol(TokenKind.]AT_TOKEN_PREFIX[EOL]AT_LOCATION_IF([[, location]])[); + case '=': return Symbol(TokenKind.]AT_TOKEN_PREFIX[EQUAL]AT_LOCATION_IF([[, location]])[); + case '^': return Symbol(TokenKind.]AT_TOKEN_PREFIX[POW]AT_LOCATION_IF([[, location]])[); + case '!': return Symbol(TokenKind.]AT_TOKEN_PREFIX[NOT]AT_LOCATION_IF([[, location]])[); + default: return Symbol(TokenKind.]AT_TOKEN_PREFIX[YYUNDEF]AT_LOCATION_IF([[, location]])[); } } } -- 2.17.1
0004-d-doc-add-code-annotation-for-yylex-definition.patch
(application/octet-stream, 1.1 KB)
From bc20202daa5da4c1b91fd1fce543179627851cbd Mon Sep 17 00:00:00 2001 From: Adela Vais <[email protected]> Date: Thu, 10 Dec 2020 23:04:21 +0200 Subject: [PATCH for Dlang support 4/9] d: doc: add @code annotation for yylex() definition in D Scanner Interface section * doc/bison.texi: Here. --- doc/bison.texi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/bison.texi b/doc/bison.texi index 314f662a..1b4fe3a4 100644 --- a/doc/bison.texi +++ b/doc/bison.texi @@ -14011,9 +14011,9 @@ This method is defined by the user to emit an error message. The first parameter is omitted if location tracking is not active. @end deftypemethod -@deftypemethod {Lexer} {Symbol} yylex() -Return the next token. The return value is of type Symbol, which -binds together the TokenKind, the semantic value and the location. +@deftypemethod {Lexer} @code{Symbol} yylex() +Return the next token. The return value is of type @code{Symbol}, which +binds together the @code{TokenKind}, the semantic value and the location. @end deftypemethod @deftypemethod {Lexer} {YYPosition} getStartPos() -- 2.17.1
0006-d-remove-yychar-from-YYParse.parse.patch
(application/octet-stream, 3.7 KB)
From dfc2657893b9ca2b65b9bda0c0d40e4f0c55dcad Mon Sep 17 00:00:00 2001 From: Adela Vais <[email protected]> Date: Fri, 11 Dec 2020 00:06:25 +0200 Subject: [PATCH for Dlang support 6/9] d: remove yychar from YYParse.parse() The yychar variable was keeping the external form of the token (the TokenKind). As the D parser translates the token to its internal form (the SymbolKind) inside the struct Symbol, there is no need for yychar anymore. * data/examples/lalr1.d: Here. --- data/skeletons/lalr1.d | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/data/skeletons/lalr1.d b/data/skeletons/lalr1.d index fe07f88d..65e183fa 100644 --- a/data/skeletons/lalr1.d +++ b/data/skeletons/lalr1.d @@ -420,8 +420,6 @@ b4_locations_if([, ref ]b4_location_type[ yylocationp])[) */ public bool parse () { - // Lookahead token kind. - int yychar = TokenKind.]b4_symbol(empty, id)[; // Lookahead symbol kind. SymbolKind yytoken = ]b4_symbol(empty, kind)[; @@ -489,17 +487,16 @@ m4_popdef([b4_at_dollar])])dnl } /* Read a lookahead token. */ - if (yychar == TokenKind.]b4_symbol(empty, id)[) + if (yytoken == ]b4_symbol(empty, kind)[) {]b4_parse_trace_if([[ yycdebugln ("Reading a token");]])[ Symbol yysymbol = yylex(); - yychar = yysymbol.token(); + yytoken = yysymbol.token(); yylval = yysymbol.value();]b4_locations_if([[ yylloc = yysymbol.location();]])[ } - /* Convert token to internal form. */ - yytoken = yytranslate_ (yychar);]b4_parse_trace_if([[ + /* Token already converted to internal form. */]b4_parse_trace_if([[ yy_symbol_print ("Next token is", yytoken, yylval]b4_locations_if([, yylloc])[);]])[ if (yytoken == ]b4_symbol(error, kind)[) @@ -508,7 +505,6 @@ m4_popdef([b4_at_dollar])])dnl // to error recovery. But do not keep the error token as // lookahead, it is too special and may lead us to an endless // loop in error recovery. */ - yychar = TokenKind.]b4_symbol(undef, id)[; yytoken = ]b4_symbol(undef, kind)[;]b4_locations_if([[ yyerrloc = yylloc;]])[ label = YYERRLAB1; @@ -543,7 +539,7 @@ m4_popdef([b4_at_dollar])])dnl yy_symbol_print ("Shifting", yytoken, yylval]b4_locations_if([, yylloc])[);]])[ /* Discard the token being shifted. */ - yychar = TokenKind.]b4_symbol(empty, id)[; + yytoken = ]b4_symbol(empty, kind)[; /* Count tokens shifted since error; after three, turn off error * status. */ @@ -586,8 +582,6 @@ m4_popdef([b4_at_dollar])])dnl if (yyerrstatus_ == 0) { ++yynerrs_; - if (yychar == TokenKind.]b4_symbol(empty, id)[) - yytoken = ]b4_symbol(empty, kind)[; yyreportSyntaxError(new Context(]b4_lac_if([[this, ]])[yystack, yytoken]b4_locations_if([[, yylloc]])[)); } ]b4_locations_if([ @@ -597,14 +591,14 @@ m4_popdef([b4_at_dollar])])dnl /* If just tried and failed to reuse lookahead token after an * error, discard it. */ - if (yychar <= TokenKind.]b4_symbol(eof, [id])[) + if (yytoken <= ]b4_symbol(eof, [kind])[) { /* Return failure if at end of input. */ - if (yychar == TokenKind.]b4_symbol(eof, [id])[) + if (yytoken == ]b4_symbol(eof, [kind])[) return false; } else - yychar = TokenKind.]b4_symbol(empty, id)[; + yytoken = ]b4_symbol(empty, kind)[; } /* Else will try to reuse lookahead token after shifting the error -- 2.17.1
0007-d-create-alias-Location-for-YYLocation.patch
(application/octet-stream, 5.9 KB)
From 94783b4df6ca47c4c4496efccff5d20389117f9a Mon Sep 17 00:00:00 2001 From: Adela Vais <[email protected]> Date: Fri, 18 Dec 2020 17:31:57 +0200 Subject: [PATCH for Dlang support 7/9] d: create alias Location for YYLocation * data/skeletons/d.m4: Here. * doc/bison.texi: Document it. * examples/d/calc/calc.y: Adjust. * tests/calc.at: Test it. --- data/skeletons/d.m4 | 11 ++++++----- doc/bison.texi | 22 +++++++++++----------- examples/d/calc/calc.y | 4 ++-- tests/calc.at | 2 +- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/data/skeletons/d.m4 b/data/skeletons/d.m4 index ee0d85a1..895ff31c 100644 --- a/data/skeletons/d.m4 +++ b/data/skeletons/d.m4 @@ -453,7 +453,8 @@ m4_define([b4_var_decl], # Depending on %define token_lex, may be output in the header or source file. m4_define([b4_public_types_declare], [[ -alias Symbol = ]b4_parser_class[.Symbol; +alias Symbol = ]b4_parser_class[.Symbol;]b4_locations_if([[ +alias Location = ]b4_location_type[;]])[ ]]) # b4_symbol_type_define @@ -469,15 +470,15 @@ m4_define([b4_symbol_type_define], { private SymbolKind kind; private ]b4_yystype[ value_;]b4_locations_if([[ - private YYLocation location_;]])[ - this(TokenKind token]b4_locations_if([[, YYLocation loc]])[) + private Location location_;]])[ + this(TokenKind token]b4_locations_if([[, Location loc]])[) { kind = yytranslate_(token);]b4_locations_if([ location_ = loc;])[ } static foreach (member; __traits(allMembers, YYSemanticType)) { - this(TokenKind token, typeof(mixin("YYSemanticType." ~ member)) val]b4_locations_if([[, YYLocation loc]])[) + this(TokenKind token, typeof(mixin("YYSemanticType." ~ member)) val]b4_locations_if([[, Location loc]])[) { kind = yytranslate_(token); mixin("value_." ~ member ~ " = val;");]b4_locations_if([ @@ -486,6 +487,6 @@ m4_define([b4_symbol_type_define], } SymbolKind token() { return kind; } ]b4_yystype[ value() { return value_; }]b4_locations_if([[ - YYLocation location() { return location_; }]])[ + Location location() { return location_; }]])[ } ]]) diff --git a/doc/bison.texi b/doc/bison.texi index 1b4fe3a4..660a1aae 100644 --- a/doc/bison.texi +++ b/doc/bison.texi @@ -13847,20 +13847,20 @@ When the directive @code{%locations} is used, the D parser supports location tracking, see @ref{Tracking Locations}. The position and the location structures are provided. -@deftypeivar {YYLocation} {YYPosition} begin -@deftypeivarx {YYLocation} {YYPosition} end +@deftypeivar {Location} {YYPosition} begin +@deftypeivarx {Location} {YYPosition} end The first, inclusive, position of the range, and the first beyond. @end deftypeivar -@deftypeop {Constructor} {YYLocation} {} this(@code{YYPosition} @var{loc}) -Create a @code{YYLocation} denoting an empty range located at a given point. +@deftypeop {Constructor} {Location} {} this(@code{YYPosition} @var{loc}) +Create a @code{Location} denoting an empty range located at a given point. @end deftypeop -@deftypeop {Constructor} {YYLocation} {} this(@code{YYPosition} @var{begin}, @code{YYPosition} @var{end}) -Create a @code{YYLocation} from the endpoints of the range. +@deftypeop {Constructor} {Location} {} this(@code{YYPosition} @var{begin}, @code{YYPosition} @var{end}) +Create a @code{Location} from the endpoints of the range. @end deftypeop -@deftypemethod {YYLocation} {string} toString() +@deftypemethod {Location} {string} toString() Prints the range represented by the location. @end deftypemethod @@ -13916,9 +13916,9 @@ which also turns on verbose error messages. @end deftypemethod @deftypemethod {YYParser} {void} yyerror(@code{string} @var{msg}) -@deftypemethodx {YYParser} {void} yyerror(@code{YYLocation} @var{loc}, @code{string} @var{msg}) +@deftypemethodx {YYParser} {void} yyerror(@code{Location} @var{loc}, @code{string} @var{msg}) Print an error message using the @code{yyerror} method of the scanner -instance in use. The @code{YYLocation} and @code{YYPosition} parameters are +instance in use. The @code{Location} and @code{YYPosition} parameters are available only if location tracking is active. @end deftypemethod @@ -14006,7 +14006,7 @@ case. In both cases, the scanner has to implement the following methods. -@deftypemethod {Lexer} {void} yyerror(@code{YYLocation} @var{loc}, @code{string} @var{msg}) +@deftypemethod {Lexer} {void} yyerror(@code{Location} @var{loc}, @code{string} @var{msg}) This method is defined by the user to emit an error message. The first parameter is omitted if location tracking is not active. @end deftypemethod @@ -16465,7 +16465,7 @@ London, Department of Computer Science, TR-00-12 (December 2000). @c LocalWords: colorYellow rgbRed colorRed rgbBlue colorBlue rgbPurple Ddoc @c LocalWords: colorPurple ifhtml ifnothtml situ rcex MERCHANTABILITY Wnone @c LocalWords: diagError diagNotice diagWarning diagOff danglingElseCex -@c LocalWords: YYLocation YYPosition nonunifying +@c LocalWords: Location YYPosition nonunifying @c Local Variables: @c ispell-dictionary: "american" diff --git a/examples/d/calc/calc.y b/examples/d/calc/calc.y index 3a8bf4eb..71c448ee 100644 --- a/examples/d/calc/calc.y +++ b/examples/d/calc/calc.y @@ -97,12 +97,12 @@ if (isInputRange!R && is(ElementType!R : dchar)) YYPosition start; YYPosition end; - YYLocation location; + Location location; // Should be a local in main, shared with %parse-param. int exit_status = 0; - void yyerror(const YYLocation loc, string s) + void yyerror(const Location loc, string s) { exit_status = 1; stderr.writeln(loc.toString(), ": ", s); diff --git a/tests/calc.at b/tests/calc.at index 9abf263d..976cea0a 100644 --- a/tests/calc.at +++ b/tests/calc.at @@ -560,7 +560,7 @@ class CalcLexer(R) : Lexer ]AT_YYERROR_DEFINE[ YYSemanticType semanticVal_;]AT_LOCATION_IF([[ - YYLocation location; + Location location; public final @property YYPosition startPos() { -- 2.17.1
0008-d-create-alias-Value-for-YYSemanticType.patch
(application/octet-stream, 6.2 KB)
From 5865c0c0c09423969cfa7b3d78254473c35ee701 Mon Sep 17 00:00:00 2001 From: Adela Vais <[email protected]> Date: Fri, 18 Dec 2020 19:46:02 +0200 Subject: [PATCH for Dlang support 8/9] d: create alias Value for YYSemanticType * data/skeletons/d.m4: Here. * data/skeletons/lalr1.d, examples/d/calc/calc.y, examples/d/simple/calc.y: Adjust. * tests/calc.at, tests/d.at, tests/scanner.at: Test it. --- data/skeletons/d.m4 | 7 ++++--- data/skeletons/lalr1.d | 14 +++++++------- examples/d/calc/calc.y | 4 ++-- examples/d/simple/calc.y | 4 ++-- tests/calc.at | 4 ++-- tests/d.at | 4 ++-- tests/scanner.at | 4 ++-- 7 files changed, 21 insertions(+), 20 deletions(-) diff --git a/data/skeletons/d.m4 b/data/skeletons/d.m4 index 895ff31c..beb7d445 100644 --- a/data/skeletons/d.m4 +++ b/data/skeletons/d.m4 @@ -453,7 +453,8 @@ m4_define([b4_var_decl], # Depending on %define token_lex, may be output in the header or source file. m4_define([b4_public_types_declare], [[ -alias Symbol = ]b4_parser_class[.Symbol;]b4_locations_if([[ +alias Symbol = ]b4_parser_class[.Symbol; +alias Value = ]b4_yystype[;]b4_locations_if([[ alias Location = ]b4_location_type[;]])[ ]]) @@ -469,7 +470,7 @@ m4_define([b4_symbol_type_define], struct Symbol { private SymbolKind kind; - private ]b4_yystype[ value_;]b4_locations_if([[ + private Value value_;]b4_locations_if([[ private Location location_;]])[ this(TokenKind token]b4_locations_if([[, Location loc]])[) { @@ -486,7 +487,7 @@ m4_define([b4_symbol_type_define], } } SymbolKind token() { return kind; } - ]b4_yystype[ value() { return value_; }]b4_locations_if([[ + Value value() { return value_; }]b4_locations_if([[ Location location() { return location_; }]])[ } ]]) diff --git a/data/skeletons/lalr1.d b/data/skeletons/lalr1.d index 65e183fa..23ec07cc 100644 --- a/data/skeletons/lalr1.d +++ b/data/skeletons/lalr1.d @@ -68,7 +68,7 @@ public interface Lexer /** * Method to retrieve the semantic value of the last scanned token. * @@return the semantic value of the last scanned token. */ - ]b4_yystype[ semanticVal (); + Value semanticVal (); /** * Entry point for the scanner. Returns the token identifier corresponding @@ -351,7 +351,7 @@ b4_user_union_members private int yyaction (int yyn, ref YYStack yystack, int yylen) { - ]b4_yystype[ yyval;]b4_locations_if([[ + Value yyval;]b4_locations_if([[ ]b4_location_type[ yyloc = yylloc_from_stack (yystack, yylen);]])[ /* If YYLEN is nonzero, implement the default value of the action: @@ -393,7 +393,7 @@ b4_user_union_members `--------------------------------*/ private final void yy_symbol_print (string s, SymbolKind yykind, - ref ]b4_yystype[ yyvaluep]dnl + ref Value yyvaluep]dnl b4_locations_if([, ref ]b4_location_type[ yylocationp])[) { if (0 < yydebug) @@ -442,7 +442,7 @@ b4_locations_if([, ref ]b4_location_type[ yylocationp])[) ]b4_location_type[ yyloc;]])[ /// Semantic value of the lookahead. - ]b4_yystype[ yylval; + Value yylval; bool yyresult;]b4_lac_if([[ // Discard the LAC context in case there still is one left from a @@ -1088,7 +1088,7 @@ m4_popdef([b4_at_dollar])])dnl private final struct YYStackElement { int state; - ]b4_yystype[ value;]b4_locations_if( + Value value;]b4_locations_if( b4_location_type[[] location;])[ } @@ -1100,7 +1100,7 @@ m4_popdef([b4_at_dollar])])dnl return stack.length; } - public final void push (int state, ]b4_yystype[ value]dnl + public final void push (int state, Value value]dnl b4_locations_if([, ref ]b4_location_type[ loc])[) { stack ~= YYStackElement(state, value]b4_locations_if([, loc])[); @@ -1127,7 +1127,7 @@ m4_popdef([b4_at_dollar])])dnl return stack[$-i-1].location; }]])[ - public final ref ]b4_yystype[ valueAt (int i) + public final ref Value valueAt (int i) { return stack[$-i-1].value; } diff --git a/examples/d/calc/calc.y b/examples/d/calc/calc.y index 71c448ee..d8f3967c 100644 --- a/examples/d/calc/calc.y +++ b/examples/d/calc/calc.y @@ -108,9 +108,9 @@ if (isInputRange!R && is(ElementType!R : dchar)) stderr.writeln(loc.toString(), ": ", s); } - YYSemanticType semanticVal_; + Value semanticVal_; - public final YYSemanticType semanticVal() + public final Value semanticVal() { return semanticVal_; } diff --git a/examples/d/simple/calc.y b/examples/d/simple/calc.y index de8da2a4..5fca647e 100644 --- a/examples/d/simple/calc.y +++ b/examples/d/simple/calc.y @@ -102,9 +102,9 @@ if (isInputRange!R && is(ElementType!R : dchar)) stderr.writeln(s); } - YYSemanticType semanticVal_; + Value semanticVal_; - public final YYSemanticType semanticVal() + public final Value semanticVal() { return semanticVal_; } diff --git a/tests/calc.at b/tests/calc.at index 976cea0a..ebd5768f 100644 --- a/tests/calc.at +++ b/tests/calc.at @@ -559,7 +559,7 @@ class CalcLexer(R) : Lexer ]AT_YYERROR_DEFINE[ - YYSemanticType semanticVal_;]AT_LOCATION_IF([[ + Value semanticVal_;]AT_LOCATION_IF([[ Location location; public final @property YYPosition startPos() @@ -572,7 +572,7 @@ class CalcLexer(R) : Lexer return location.end; } ]])[ - public final @property YYSemanticType semanticVal() + public final @property Value semanticVal() { return semanticVal_; } diff --git a/tests/d.at b/tests/d.at index 3e83a8af..07de11bc 100644 --- a/tests/d.at +++ b/tests/d.at @@ -78,8 +78,8 @@ class CalcLexer(R) : Lexer void yyerror(string s) {} - YYSemanticType semanticVal_; - YYSemanticType semanticVal() @property { return semanticVal_; } + Value semanticVal_; + Value semanticVal() @property { return semanticVal_; } Symbol yylex() { diff --git a/tests/scanner.at b/tests/scanner.at index d0d3f390..e55ad3b6 100644 --- a/tests/scanner.at +++ b/tests/scanner.at @@ -115,8 +115,8 @@ class YYLexer(R) : Lexer ]AT_YYERROR_DEFINE[ - YYSemanticType semanticVal_; - public final @property YYSemanticType semanticVal () + Value semanticVal_; + public final @property Value semanticVal () { return semanticVal_; } -- 2.17.1
0009-d-create-alias-Position-for-YYPosition.patch
(application/octet-stream, 5.4 KB)
From d09cc0f103a824596340d8bbaff38ecd795ba8b1 Mon Sep 17 00:00:00 2001 From: Adela Vais <[email protected]> Date: Fri, 18 Dec 2020 19:53:32 +0200 Subject: [PATCH for Dlang support 9/9] d: create alias Position for YYPosition * data/skeletons/d.m4: Here. * data/skeletons/lalr1.d: Adjust. * doc/bison.texi: Document it. * examples/d/calc/calc.y: Use it. * tests/calc.at: Test it. --- data/skeletons/d.m4 | 3 ++- data/skeletons/lalr1.d | 4 ++-- doc/bison.texi | 16 ++++++++-------- examples/d/calc/calc.y | 8 ++++---- tests/calc.at | 4 ++-- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/data/skeletons/d.m4 b/data/skeletons/d.m4 index beb7d445..4e4d6aff 100644 --- a/data/skeletons/d.m4 +++ b/data/skeletons/d.m4 @@ -455,7 +455,8 @@ m4_define([b4_public_types_declare], [[ alias Symbol = ]b4_parser_class[.Symbol; alias Value = ]b4_yystype[;]b4_locations_if([[ -alias Location = ]b4_location_type[;]])[ +alias Location = ]b4_location_type[; +alias Position = ]b4_position_type[;]])[ ]]) # b4_symbol_type_define diff --git a/data/skeletons/lalr1.d b/data/skeletons/lalr1.d index 23ec07cc..4a2dff38 100644 --- a/data/skeletons/lalr1.d +++ b/data/skeletons/lalr1.d @@ -57,12 +57,12 @@ public interface Lexer /** * Method to retrieve the beginning position of the last scanned token. * @@return the position at which the last scanned token starts. */ - ]b4_position_type[ startPos (); + Position startPos (); /** * Method to retrieve the ending position of the last scanned token. * @@return the first position beyond the last scanned token. */ - ]b4_position_type[ endPos (); + Position endPos (); ]])[ /** diff --git a/doc/bison.texi b/doc/bison.texi index 660a1aae..0eadc837 100644 --- a/doc/bison.texi +++ b/doc/bison.texi @@ -13847,16 +13847,16 @@ When the directive @code{%locations} is used, the D parser supports location tracking, see @ref{Tracking Locations}. The position and the location structures are provided. -@deftypeivar {Location} {YYPosition} begin -@deftypeivarx {Location} {YYPosition} end +@deftypeivar {Location} {Position} begin +@deftypeivarx {Location} {Position} end The first, inclusive, position of the range, and the first beyond. @end deftypeivar -@deftypeop {Constructor} {Location} {} this(@code{YYPosition} @var{loc}) +@deftypeop {Constructor} {Location} {} this(@code{Position} @var{loc}) Create a @code{Location} denoting an empty range located at a given point. @end deftypeop -@deftypeop {Constructor} {Location} {} this(@code{YYPosition} @var{begin}, @code{YYPosition} @var{end}) +@deftypeop {Constructor} {Location} {} this(@code{Position} @var{begin}, @code{Position} @var{end}) Create a @code{Location} from the endpoints of the range. @end deftypeop @@ -13918,7 +13918,7 @@ which also turns on verbose error messages. @deftypemethod {YYParser} {void} yyerror(@code{string} @var{msg}) @deftypemethodx {YYParser} {void} yyerror(@code{Location} @var{loc}, @code{string} @var{msg}) Print an error message using the @code{yyerror} method of the scanner -instance in use. The @code{Location} and @code{YYPosition} parameters are +instance in use. The @code{Location} and @code{Position} parameters are available only if location tracking is active. @end deftypemethod @@ -14016,8 +14016,8 @@ Return the next token. The return value is of type @code{Symbol}, which binds together the @code{TokenKind}, the semantic value and the location. @end deftypemethod -@deftypemethod {Lexer} {YYPosition} getStartPos() -@deftypemethodx {Lexer} {YYPosition} getEndPos() +@deftypemethod {Lexer} {Position} getStartPos() +@deftypemethodx {Lexer} {Position} getEndPos() Return respectively the first position of the last token that @code{yylex} returned, and the first position beyond it. These methods are not needed unless location tracking is active. @@ -16465,7 +16465,7 @@ London, Department of Computer Science, TR-00-12 (December 2000). @c LocalWords: colorYellow rgbRed colorRed rgbBlue colorBlue rgbPurple Ddoc @c LocalWords: colorPurple ifhtml ifnothtml situ rcex MERCHANTABILITY Wnone @c LocalWords: diagError diagNotice diagWarning diagOff danglingElseCex -@c LocalWords: Location YYPosition nonunifying +@c LocalWords: Location Position nonunifying @c Local Variables: @c ispell-dictionary: "american" diff --git a/examples/d/calc/calc.y b/examples/d/calc/calc.y index d8f3967c..c4ff89a3 100644 --- a/examples/d/calc/calc.y +++ b/examples/d/calc/calc.y @@ -95,8 +95,8 @@ if (isInputRange!R && is(ElementType!R : dchar)) this(R r) { input = r; } - YYPosition start; - YYPosition end; + Position start; + Position end; Location location; // Should be a local in main, shared with %parse-param. @@ -170,12 +170,12 @@ if (isInputRange!R && is(ElementType!R : dchar)) } } - YYPosition startPos() const + Position startPos() const { return start; } - YYPosition endPos() const + Position endPos() const { return end; } diff --git a/tests/calc.at b/tests/calc.at index ebd5768f..75b64e10 100644 --- a/tests/calc.at +++ b/tests/calc.at @@ -562,12 +562,12 @@ class CalcLexer(R) : Lexer Value semanticVal_;]AT_LOCATION_IF([[ Location location; - public final @property YYPosition startPos() + public final @property Position startPos() { return location.begin; } - public final @property YYPosition endPos() + public final @property Position endPos() { return location.end; } -- 2.17.1