From 4a42a4f9116a4ed36966751cf597a58340181c68 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Fri, 30 Nov 2018 06:18:17 +0100 Subject: d: add skeleton for the D language Contributed by Oliver Mangold. https://lists.gnu.org/archive/html/help-bison/2012-01/msg00000.html * README-D.txt, d-skel.m4, d.m4, lalr1.d: New. --- THANKS | 1 + data/README-D.txt | 60 ++++ data/d-skel.m4 | 26 ++ data/d.m4 | 298 +++++++++++++++++ data/lalr1.d | 977 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ data/local.mk | 7 + 6 files changed, 1369 insertions(+) create mode 100644 data/README-D.txt create mode 100644 data/d-skel.m4 create mode 100644 data/d.m4 create mode 100644 data/lalr1.d diff --git a/THANKS b/THANKS index 3d61e148..cb0c5737 100644 --- a/THANKS +++ b/THANKS @@ -121,6 +121,7 @@ Noah Friedman friedman@gnu.org Odd Arild Olsen oao@fibula.no Oleg Smolsky oleg.smolsky@pacific-simulators.co.nz Oleksii Taran oleksii.taran@gmail.com +Oliver Mangold o.mangold@gmail.com Paolo Bonzini bonzini@gnu.org Paolo Simone Gasparello djgaspa@gmail.com Pascal Bart pascal.bart@epita.fr diff --git a/data/README-D.txt b/data/README-D.txt new file mode 100644 index 00000000..171187e7 --- /dev/null +++ b/data/README-D.txt @@ -0,0 +1,60 @@ +Some usage notes for the D Parser: + +- it is a port of the Java parser, so interface is very similar. + +- the lexer class needs to implement the interface 'Lexer' (similar to + java). It typically (depending on options) looks like this: + +public interface Lexer +{ + /** + * Method to retrieve the beginning position of the last scanned token. + * @return the position at which the last scanned token starts. */ + @property YYPosition startPos (); + + /** + * Method to retrieve the ending position of the last scanned token. + * @return the first position beyond the last scanned token. */ + @property YYPosition endPos (); + + /** + * Method to retrieve the semantic value of the last scanned token. + * @return the semantic value of the last scanned token. */ + @property YYSemanticType semanticVal (); + + /** + * Entry point for the scanner. Returns the token identifier corresponding + * to the next token and prepares to return the semantic value + * and beginning/ending positions of the token. + * @return the token identifier corresponding to the next token. */ + YYTokenType yylex (); + + /** + * Entry point for error reporting. Emits an error + * referring to the given location in a user-defined way. + * + * @param loc The location of the element to which the + * error message is related + * @param s The string for the error message. */ + void yyerror (YYLocation loc, string s); +} + +- semantic types are handled by D usions (same as for C/C++ parsers) + +- the following (non-standard) %defines are supported: + + %define package "" + %define parser_class_name "my_class_name>" + %define position_type "my_position_type" + %define location_type "my_location_type" + +- the following declarations basically work like in C/C++: + + %locations + %error-verbose + %parse-param + %initial-action + %code + %union + +- %destructor is not yet supported diff --git a/data/d-skel.m4 b/data/d-skel.m4 new file mode 100644 index 00000000..0c84a988 --- /dev/null +++ b/data/d-skel.m4 @@ -0,0 +1,26 @@ + -*- Autoconf -*- + +# D skeleton dispatching for Bison. + +# Copyright (C) 2018 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +b4_glr_if( [b4_complain([%%glr-parser not supported for D])]) +b4_nondeterministic_if([b4_complain([%%nondeterministic-parser not supported for D])]) + +m4_define_default([b4_used_skeleton], [b4_pkgdatadir/[lalr1.d]]) +m4_define_default([b4_skeleton], ["b4_basename(b4_used_skeleton)"]) + +m4_include(b4_used_skeleton) diff --git a/data/d.m4 b/data/d.m4 new file mode 100644 index 00000000..abd0f359 --- /dev/null +++ b/data/d.m4 @@ -0,0 +1,298 @@ + -*- Autoconf -*- + +# D language support for Bison + +# Copyright (C) 2018 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +# b4_comment(TEXT) +# ---------------- +m4_define([b4_comment], [/* m4_bpatsubst([$1], [ +], [ + ]) */]) + + +# b4_list2(LIST1, LIST2) +# ---------------------- +# Join two lists with a comma if necessary. +m4_define([b4_list2], + [$1[]m4_ifval(m4_quote($1), [m4_ifval(m4_quote($2), [[, ]])])[]$2]) + + +# b4_percent_define_get3(DEF, PRE, POST, NOT) +# ------------------------------------------- +# Expand to the value of DEF surrounded by PRE and POST if it's %define'ed, +# otherwise NOT. +m4_define([b4_percent_define_get3], + [m4_ifval(m4_quote(b4_percent_define_get([$1])), + [$2[]b4_percent_define_get([$1])[]$3], [$4])]) + + +# b4_flag_value(BOOLEAN-FLAG) +# --------------------------- +m4_define([b4_flag_value], [b4_flag_if([$1], [true], [false])]) + + +# b4_public_if(TRUE, FALSE) +# ------------------------- +b4_percent_define_default([[public]], [[false]]) +m4_define([b4_public_if], +[b4_percent_define_flag_if([public], [$1], [$2])]) + + +# b4_abstract_if(TRUE, FALSE) +# --------------------------- +b4_percent_define_default([[abstract]], [[false]]) +m4_define([b4_abstract_if], +[b4_percent_define_flag_if([abstract], [$1], [$2])]) + + +# b4_final_if(TRUE, FALSE) +# --------------------------- +b4_percent_define_default([[final]], [[false]]) +m4_define([b4_final_if], +[b4_percent_define_flag_if([final], [$1], [$2])]) + + +# b4_lexer_if(TRUE, FALSE) +# ------------------------ +m4_define([b4_lexer_if], +[b4_percent_code_ifdef([[lexer]], [$1], [$2])]) + + +# b4_position_type_if(TRUE, FALSE) +# ------------------------ +m4_define([b4_position_type_if], +[b4_percent_define_ifdef([[position_type]], [$1], [$2])]) + + +# b4_location_type_if(TRUE, FALSE) +# ------------------------ +m4_define([b4_location_type_if], +[b4_percent_define_ifdef([[location_type]], [$1], [$2])]) + + +# b4_locations_if(TRUE, FALSE) +m4_define([b4_locations_if], +[m4_if(b4_locations_flag,1,[$1],[$2])]) + +# b4_identification +# ----------------- +m4_define([b4_identification], +[/** Version number for the Bison executable that generated this parser. */ + public static immutable string yy_bison_version = "b4_version"; + + /** Name of the skeleton that generated this parser. */ + public static immutable string yy_bison_skeleton = b4_skeleton; +]) + + +## ------------ ## +## Data types. ## +## ------------ ## + +# b4_int_type(MIN, MAX) +# --------------------- +# Return the smallest int type able to handle numbers ranging from +# MIN to MAX (included). +m4_define([b4_int_type], +[m4_if(b4_ints_in($@, [-128], [127]), [1], [byte], + b4_ints_in($@, [-32768], [32767]), [1], [short], + [int])]) + +# b4_int_type_for(NAME) +# --------------------- +# Return the smallest int type able to handle numbers ranging from +# `NAME_min' to `NAME_max' (included). +m4_define([b4_int_type_for], +[b4_int_type($1_min, $1_max)]) + +# b4_null +# ------- +m4_define([b4_null], [null]) + + +## ------------------------- ## +## Assigning token numbers. ## +## ------------------------- ## + +# b4_token_enum(TOKEN-NAME, TOKEN-NUMBER) +# --------------------------------------- +# Output the definition of this token as an enum. +m4_define([b4_token_enum], +[$1 = $2]) + +# b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER) +# ----------------------------------------------------- +# Output the definition of the tokens as enums. +m4_define([b4_token_enums], +[/* Tokens. */ +public enum YYTokenType { + + /** Token returned by the scanner to signal the end of its input. */ + EOF = 0, + +m4_map_sep([ b4_token_enum], [, +], + [$@]) +} +]) + +# b4-case(ID, CODE) +# ----------------- +m4_define([b4_case], [ case $1: +$2 + break;]) + + +## ---------------- ## +## Default values. ## +## ---------------- ## + +m4_define([b4_yystype], [b4_percent_define_get([[stype]])]) +b4_percent_define_default([[stype]], [[YYSemanticType]])]) + +# %name-prefix +m4_define_default([b4_prefix], [[YY]]) + +b4_percent_define_default([[parser_class_name]], [b4_prefix[]YYParser])]) +m4_define([b4_parser_class_name], [b4_percent_define_get([[parser_class_name]])]) + +#b4_percent_define_default([[location_type]], [Location])]) +m4_define([b4_location_type], b4_percent_define_ifdef([[location_type]],[b4_percent_define_get([[location_type]])],[YYLocation])) + +#b4_percent_define_default([[position_type]], [Position])]) +m4_define([b4_position_type], b4_percent_define_ifdef([[position_type]],[b4_percent_define_get([[position_type]])],[YYPosition])) + + +## ----------------- ## +## Semantic Values. ## +## ----------------- ## + + +# b4_lhs_value([TYPE]) +# -------------------- +# Expansion of $$. +m4_define([b4_lhs_value], [(yyval]m4_ifval($1, [.$1])[)]) + + +# b4_rhs_value(RULE-LENGTH, NUM, [TYPE]) +# -------------------------------------- +# Expansion of $NUM, where the current rule has RULE-LENGTH +# symbols on RHS. +# +# In this simple implementation, %token and %type have class names +# between the angle brackets. +m4_define([b4_rhs_value], +[((yystack.valueAt ($1-($2)))m4_ifval($3, [.$3]))]) + +# b4_lhs_location() +# ----------------- +# Expansion of @$. +m4_define([b4_lhs_location], +[(yyloc)]) + + +# b4_rhs_location(RULE-LENGTH, NUM) +# --------------------------------- +# Expansion of @NUM, where the current rule has RULE-LENGTH symbols +# on RHS. +m4_define([b4_rhs_location], +[yystack.locationAt ($1-($2))]) + + +# b4_lex_param +# b4_parse_param +# -------------- +# If defined, b4_lex_param arrives double quoted, but below we prefer +# it to be single quoted. Same for b4_parse_param. + +# TODO: should be in bison.m4 +m4_define_default([b4_lex_param], [[]])) +m4_define([b4_lex_param], b4_lex_param)) +m4_define([b4_parse_param], b4_parse_param)) + +# b4_lex_param_decl +# ------------------- +# Extra formal arguments of the constructor. +m4_define([b4_lex_param_decl], +[m4_ifset([b4_lex_param], + [b4_remove_comma([$1], + b4_param_decls(b4_lex_param))], + [$1])]) + +m4_define([b4_param_decls], + [m4_map([b4_param_decl], [$@])]) +m4_define([b4_param_decl], [, $1]) + +m4_define([b4_remove_comma], [m4_ifval(m4_quote($1), [$1, ], [])m4_shift2($@)]) + + + +# b4_parse_param_decl +# ------------------- +# Extra formal arguments of the constructor. +m4_define([b4_parse_param_decl], +[m4_ifset([b4_parse_param], + [b4_remove_comma([$1], + b4_param_decls(b4_parse_param))], + [$1])]) + + + +# b4_lex_param_call +# ------------------- +# Delegating the lexer parameters to the lexer constructor. +m4_define([b4_lex_param_call], + [m4_ifset([b4_lex_param], + [b4_remove_comma([$1], + b4_param_calls(b4_lex_param))], + [$1])]) +m4_define([b4_param_calls], + [m4_map([b4_param_call], [$@])]) +m4_define([b4_param_call], [, $2]) + + + +# b4_parse_param_cons +# ------------------- +# Extra initialisations of the constructor. +m4_define([b4_parse_param_cons], + [m4_ifset([b4_parse_param], + [b4_constructor_calls(b4_parse_param)])]) + +m4_define([b4_constructor_calls], + [m4_map([b4_constructor_call], [$@])]) +m4_define([b4_constructor_call], + [this.$2 = $2; + ]) + + + +# b4_parse_param_vars +# ------------------- +# Extra instance variables. +m4_define([b4_parse_param_vars], + [m4_ifset([b4_parse_param], + [ + /* User arguments. */ +b4_var_decls(b4_parse_param)])]) + +m4_define([b4_var_decls], + [m4_map_sep([b4_var_decl], [ +], [$@])]) +m4_define([b4_var_decl], + [ protected $1;]) diff --git a/data/lalr1.d b/data/lalr1.d new file mode 100644 index 00000000..2f42cce6 --- /dev/null +++ b/data/lalr1.d @@ -0,0 +1,977 @@ +# Java skeleton for Bison -*- autoconf -*- + +# Copyright (C) 2007-2011 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +m4_include([d.m4]) + + +m4_divert_push(0)dnl +@output(b4_parser_file_name@)@ +b4_copyright([Skeleton implementation for Bison LALR(1) parsers in D], + [2007-2012]) + +b4_percent_define_ifdef([package], [module b4_percent_define_get([package]); +])[ +version(D_Version2) { +} else { + static assert(false,"need compiler for D Version 2"); +} + +/* First part of user declarations. */ +]b4_pre_prologue +b4_percent_code_get([[imports]]) +[static import std.stream; + +/** + * A Bison parser, automatically generated from ]m4_bpatsubst(b4_file_name, [^"\(.*\)"$], [\1])[. + * + * @@author LALR (1) parser skeleton written by Paolo Bonzini. + * Port to D language was done by Oliver Mangold. + */ + +/** + * Communication interface between the scanner and the Bison-generated + * parser ]b4_parser_class_name[. + */ +public interface Lexer +{ + ]b4_locations_if([[/** + * Method to retrieve the beginning position of the last scanned token. + * @@return the position at which the last scanned token starts. */ + @@property ]b4_position_type[ startPos (); + + /** + * Method to retrieve the ending position of the last scanned token. + * @@return the first position beyond the last scanned token. */ + @@property ]b4_position_type[ endPos ();]])[ + + /** + * Method to retrieve the semantic value of the last scanned token. + * @@return the semantic value of the last scanned token. */ + @@property ]b4_yystype[ semanticVal (); + + /** + * Entry point for the scanner. Returns the token identifier corresponding + * 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. */ + YYTokenType yylex (); + + /** + * Entry point for error reporting. Emits an error + * ]b4_locations_if([referring to the given location ])[in a user-defined way. + * + * ]b4_locations_if([[@@param loc The location of the element to which the + * error message is related]])[ + * @@param s The string for the error message. */ + void yyerror (]b4_locations_if([b4_location_type[ loc, ]])[string s); +} + +private final struct YYStackElement{ + int state; + ]b4_yystype[ value; + ]b4_locations_if(b4_location_type[[] location;])[ +} + +private final struct YYStack { + private YYStackElement[] stack = []; + + public final @@property ulong height() + { + return stack.length; + } + + public final void push (int state, ]b4_yystype[ value]dnl +b4_locations_if([, ref ]b4_location_type[ loc])[) + { + stack ~= YYStackElement(state, value]b4_locations_if([, loc])[); + } + + public final void pop () + { + pop (1); + } + + public final void pop (int num) + { + stack.length -= num; + } + + public final int stateAt (int i) + { + return stack[$-i-1].state; + } + + ]b4_locations_if([[public final ref ]b4_location_type[ locationAt (int i) + { + return stack[$-i-1].location; + } + + ]])[public final ref ]b4_yystype[ valueAt (int i) + { + return stack[$-i-1].value; + } + + // Print the state stack on the debug stream. + public final void print (std.stream.OutputStream stream) + { + stream.writeString ("Stack now"); + + for (int i = 0; i < stack.length; i++) + { + stream.write (' '); + stream.writef ("%d",stack[i].state); + } + stream.writefln (); + } +} +]b4_locations_if(b4_position_type_if([[[ +static assert(__traits(compiles, + (new ]b4_position_type[[1])[0]=(new ]b4_position_type[[1])[0]), + "struct/class ]b4_position_type[ must be default-constructible " + "and assignable"); +static assert(__traits(compiles,(new string[1])[0]=(new ]b4_position_type[).toString()), + "error: struct/class ]b4_position_type[ must have toString method"); +]]],[[ + /** + * A struct denoting a point in the input.*/ +public struct ]b4_position_type[ { + + /** The column index within the line of input. */ + public int column = 1; + /** The line number within an input file. */ + public int line = 1; + /** The name of the input file. */ + public string filename = "(unspecified file)"; + + /** + * Return a string representation of the position. */ + public string toString() const { + return std.string.format("%s:%d.%d",filename,line,column); + } +} +]])b4_location_type_if([[[ +static assert(__traits(compiles,(new ]b4_location_type[((new ]b4_position_type[[1])[0]))) && + __traits(compiles,(new ]b4_location_type[((new ]b4_position_type[[1])[0],(new ]b4_position_type[[1])[0]))), + "error: struct/class ]b4_location_type[ must have " + "default constructor and constructors this(]b4_position_type[) and this(]b4_position_type[,]b4_position_type[)."); +static assert(__traits(compiles,(new ]b4_location_type[[1])[0].begin=(new ]b4_location_type[[1])[0].begin) && + __traits(compiles,(new ]b4_location_type[[1])[0].begin=(new ]b4_location_type[[1])[0].end) && + __traits(compiles,(new ]b4_location_type[[1])[0].end=(new ]b4_location_type[[1])[0].begin) && + __traits(compiles,(new ]b4_location_type[[1])[0].end=(new ]b4_location_type[[1])[0].end), + "error: struct/class ]b4_location_type[ must have assignment-compatible " + "members/properties 'begin' and 'end'."); +static assert(__traits(compiles,(new string[1])[0]=(new ]b4_location_type[[1])[0].toString()), + "error: struct/class ]b4_location_type[ must have toString method."); + +private immutable bool yy_location_is_class = !__traits(compiles,*(new ]b4_location_type[((new ]b4_position_type[[1])[0])));]]],[[ +/** + * A class defining a pair of positions. Positions, defined by the + * ]b4_position_type[ class, denote a point in the input. + * Locations represent a part of the input through the beginning + * and ending positions. */ +public class ]b4_location_type[ +{ + /** The first, inclusive, position in the range. */ + public ]b4_position_type[ begin; + + /** The first position beyond the range. */ + public ]b4_position_type[ end; + + /** + * Create a ]b4_location_type[ denoting an empty range located at + * a given point. + * @@param loc The position at which the range is anchored. */ + public this (]b4_position_type[ loc) { + this.begin = this.end = loc; + } + + /** + * Create a ]b4_location_type[ from the endpoints of the range. + * @@param begin The first position included in the range. + * @@param end The first position beyond the range. */ + public this (]b4_position_type[ begin, ]b4_position_type[ end) + { + this.begin = begin; + this.end = end; + } + + /** + * Return a representation of the location. For this to be correct, + * ]b4_position_type[ should override the toString + * method. */ + public const string toString () const { + if (begin==end) + return begin.toString (); + else + return begin.toString () ~ "-" ~ end.toString (); + } +} + +private immutable bool yy_location_is_class = true; + +]]))m4_ifdef([b4_stype],[private union YYSemanticType +{ +b4_user_stype +}; + +]m4_if(b4_tag_seen_flag, 0, +[[private alias int YYSemanticType;]])) +b4_token_enums(b4_tokens)[ +]b4_public_if([public ])dnl +b4_abstract_if([abstract ])dnl +b4_final_if([final ])dnl +[class ]b4_parser_class_name[]dnl +b4_percent_define_get3([extends], [ extends ])dnl +b4_percent_define_get3([implements], [ implements ])[ +{ + ]b4_identification[ + + /** True if verbose error messages are enabled. */ + public bool errorVerbose = ]b4_flag_value([error_verbose]); + + b4_locations_if([[ + private final ]b4_location_type[ yylloc_from_stack (ref YYStack rhs, int n) + { + static if (yy_location_is_class) { + if (n > 0) + return new ]b4_location_type[ (rhs.locationAt (n-1).begin, rhs.locationAt (0).end); + else + return new ]b4_location_type[ (rhs.locationAt (0).end); + } else { + if (n > 0) + return ]b4_location_type[ (rhs.locationAt (n-1).begin, rhs.locationAt (0).end); + else + return ]b4_location_type[ (rhs.locationAt (0).end); + } + }]])[ + + ]b4_lexer_if([[private class YYLexer implements Lexer { +]b4_percent_code_get([[lexer]])[ + } + + ]])[/** The object doing lexical analysis for us. */ + private Lexer yylexer; + ] + b4_parse_param_vars + +b4_lexer_if([[ + /** + * Instantiates the Bison-generated parser. + */ + public this] (b4_parse_param_decl([b4_lex_param_decl])[) { + this.yylexer = new YYLexer(]b4_lex_param_call[); + this.yyDebugStream = std.cstream.derr; + ]b4_parse_param_cons[ + } +]]) + + /** + * Instantiates the Bison-generated parser. + * @@param yylexer The scanner that will supply tokens to the parser. + */ + b4_lexer_if([[protected]], [[public]]) [this (]b4_parse_param_decl([[Lexer yylexer]])[) { + this.yylexer = yylexer; + this.yyDebugStream = std.cstream.derr; + ]b4_parse_param_cons[ + } + + private std.stream.OutputStream yyDebugStream; + + /** + * Return the std.stream.OutputStream on which the debugging output is + * printed. + */ + public std.stream.OutputStream getDebugStream () { return yyDebugStream; } + + /** + * Set the std.stream.OutputStream on which the debug output is printed. + * @@param s The stream that is used for debugging output. + */ + public final void setDebugStream(std.stream.OutputStream s) { yyDebugStream = s; } + + private int yydebug = 0; + + /** + * Answer the verbosity of the debugging output; 0 means that all kinds of + * output from the parser are suppressed. + */ + public final int getDebugLevel() { return yydebug; } + + /** + * Set the verbosity of the debugging output; 0 means that all kinds of + * output from the parser are suppressed. + * @@param level The verbosity level for debugging output. + */ + public final void setDebugLevel(int level) { yydebug = level; } + + private final int yylex () { + return yylexer.yylex (); + } + protected final void yyerror (]b4_locations_if(ref [b4_location_type[ loc, ]])[string s) { + yylexer.yyerror (]b4_locations_if([loc, ])[s); + }] + + [protected final void yycdebug (string s) { + if (yydebug > 0) + yyDebugStream.writeLine (s); + } + + /** + * Returned by a Bison action in order to stop the parsing process and + * return success (true). */ + public static immutable int YYACCEPT = 0; + + /** + * Returned by a Bison action in order to stop the parsing process and + * return failure (false). */ + public static immutable int YYABORT = 1; + + /** + * Returned by a Bison action in order to start error recovery without + * printing an error message. */ + public static immutable int YYERROR = 2; + + // Internal return codes that are not supported for user semantic + // actions. + private static immutable int YYERRLAB = 3; + private static immutable int YYNEWSTATE = 4; + private static immutable int YYDEFAULT = 5; + private static immutable int YYREDUCE = 6; + private static immutable int YYERRLAB1 = 7; + private static immutable int YYRETURN = 8; + + private static immutable YYSemanticType yy_semantic_null = cast(YYSemanticType)null; + private int yyerrstatus_ = 0; + + /** + * Return whether error recovery is being done. In this state, the parser + * reads token until it reaches a known state, and then restarts normal + * operation. */ + public final bool recovering () + { + return yyerrstatus_ == 0; + } + + private int yyaction (int yyn, ref YYStack yystack, int yylen) + { + ]b4_yystype[ yyval; + ]b4_locations_if([b4_location_type[ yyloc = yylloc_from_stack (yystack, yylen);]])[ + + /* If YYLEN is nonzero, implement the default value of the action: + `$$ = $1'. Otherwise, use the top of the stack. + + Otherwise, the following line sets YYVAL to garbage. + This behavior is undocumented and Bison + users should not rely upon it. */ + if (yylen > 0) + yyval = yystack.valueAt (yylen - 1); + else + yyval = yystack.valueAt (0); + + yy_reduce_print (yyn, yystack); + + switch (yyn) + { +]b4_user_actions[ + default: break; + } + + yy_symbol_print ("-> $$ =", yyr1_[yyn], yyval]b4_locations_if([, yyloc])[); + + yystack.pop (yylen); + yylen = 0; + + /* Shift the result of the reduction. */ + yyn = yyr1_[yyn]; + int yystate = yypgoto_[yyn - yyntokens_] + yystack.stateAt (0); + if (0 <= yystate && yystate <= yylast_ + && yycheck_[yystate] == yystack.stateAt (0)) + yystate = yytable_[yystate]; + else + yystate = yydefgoto_[yyn - yyntokens_]; + + yystack.push (yystate, yyval]b4_locations_if([, yyloc])[); + return YYNEWSTATE; + } + + /* Return YYSTR after stripping away unnecessary quotes and + backslashes, so that it's suitable for yyerror. The heuristic is + that double-quoting is unnecessary unless the string contains an + apostrophe, a comma, or backslash (other than backslash-backslash). + YYSTR is taken from yytname. */ + private final string yytnamerr_ (string yystr) + { + if (yystr[0] == '"') + { + string yyr; + strip_quotes: for (int i = 1; i < yystr.length; i++) + switch (yystr[i]) + { + case '\'': + case ',': + break strip_quotes; + + case '\\': + if (yystr[++i] != '\\') + break strip_quotes; + /* Fall through. */ + default: + yyr ~= yystr[i]; + break; + + case '"': + return yyr; + } + } + else if (yystr=="$end") + return "end of input"; + + return yystr; + } + + /*--------------------------------. + | Print this symbol on YYOUTPUT. | + `--------------------------------*/ + + private final void yy_symbol_print (string s, int yytype, + ref ]b4_yystype[ yyvaluep]dnl +b4_locations_if([, ref ]b4_location_type[ yylocationp])[) + { + if (yydebug > 0) { + string message = s ~ (yytype < yyntokens_ ? " token " : " nterm ") + ~ yytname_[yytype] ~ " ("]b4_locations_if([ + ~ yylocationp.toString() ~ ": "])[; + static if (__traits(compiles,message~=yyvaluep.toString ())) + message ~= yyvaluep.toString (); + else + message ~= std.string.format ("%s",&yyvaluep); + message ~= ")"; + yycdebug (message); + } + } + + /** + * Parse input from the scanner that was specified at object construction + * time. Return whether the end of the input was reached successfully. + * + * @@return true if the parsing succeeds. Note that this does not + * imply that there were no syntax errors. + */ + public bool parse () + { + /// Lookahead and lookahead in internal form. + int yychar = yyempty_; + int yytoken = 0; + + /* State. */ + int yyn = 0; + int yylen = 0; + int yystate = 0; + + YYStack yystack; + + /* Error handling. */ + int yynerrs_ = 0; + ]b4_locations_if([/// The location where the error started. + ]b4_location_type[ yyerrloc = null; + + /// ]b4_location_type[ of the lookahead. + ]b4_location_type[ yylloc; + + /// @@$. + ]b4_location_type[ yyloc;])[ + + /// Semantic value of the lookahead. + ]b4_yystype[ yylval; + + int yyresult; + + yycdebug ("Starting parse\n"); + yyerrstatus_ = 0; + +]m4_ifdef([b4_initial_action], [ +m4_pushdef([b4_at_dollar], [yylloc])dnl +m4_pushdef([b4_dollar_dollar], [yylval])dnl + /* User initialization code. */ + b4_user_initial_action +m4_popdef([b4_dollar_dollar])dnl +m4_popdef([b4_at_dollar])])dnl + + [ /* Initialize the stack. */ + yystack.push (yystate, yylval]b4_locations_if([, yylloc])[); + + int label = YYNEWSTATE; + for (;;) + final switch (label) + { + /* New state. Unlike in the C/C++ skeletons, the state is already + pushed when we come here. */ + case YYNEWSTATE: + yycdebug (std.string.format("Entering state %d\n",yystate)); + if (yydebug > 0) + yystack.print (yyDebugStream); + + /* Accept? */ + if (yystate == yyfinal_) + return true; + + /* Take a decision. First try without lookahead. */ + yyn = yypact_[yystate]; + if (yy_pact_value_is_default_ (yyn)) + { + label = YYDEFAULT; + break; + } + + /* Read a lookahead token. */ + if (yychar == yyempty_) + { + yycdebug ("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;[ + } + + /* Convert token to internal form. */ + if (yychar <= YYTokenType.EOF) + { + yychar = yytoken = YYTokenType.EOF; + yycdebug ("Now at end of input.\n"); + } + else + { + yytoken = yytranslate_ (yychar); + yy_symbol_print ("Next token is", + yytoken,yylval]b4_locations_if([, yylloc])[); + } + + /* If the proper action on seeing token YYTOKEN is to reduce or to + detect an error, take that action. */ + yyn += yytoken; + if (yyn < 0 || yylast_ < yyn || yycheck_[yyn] != yytoken) + label = YYDEFAULT; + + /* <= 0 means reduce or error. */ + else if ((yyn = yytable_[yyn]) <= 0) + { + if (yy_table_value_is_error_ (yyn)) + label = YYERRLAB; + else + { + yyn = -yyn; + label = YYREDUCE; + } + } + else + { + /* Shift the lookahead token. */ + yy_symbol_print ("Shifting", yytoken, + yylval]b4_locations_if([, yylloc])[); + + /* Discard the token being shifted. */ + yychar = yyempty_; + + /* Count tokens shifted since error; after three, turn off error + * status. */ + if (yyerrstatus_ > 0) + --yyerrstatus_; + + yystate = yyn; + yystack.push (yystate, yylval]b4_locations_if([, yylloc])[); + label = YYNEWSTATE; + } + break; + + /*-----------------------------------------------------------. + | yydefault -- do the default action for the current state. | + `-----------------------------------------------------------*/ + case YYDEFAULT: + yyn = yydefact_[yystate]; + if (yyn == 0) + label = YYERRLAB; + else + label = YYREDUCE; + break; + + /*-----------------------------. + | yyreduce -- Do a reduction. | + `-----------------------------*/ + case YYREDUCE: + yylen = yyr2_[yyn]; + label = yyaction (yyn, yystack, yylen); + yystate = yystack.stateAt (0); + break; + + /*------------------------------------. + | yyerrlab -- here on detecting error | + `------------------------------------*/ + case YYERRLAB: + /* If not already recovering from an error, report this error. */ + if (yyerrstatus_ == 0) + { + ++yynerrs_; + if (yychar == yyempty_) + yytoken = yyempty_; + yyerror (]b4_locations_if([yylloc, ])[yysyntax_error (yystate, yytoken)); + } + + ]b4_locations_if([yyerrloc = yylloc;])[ + if (yyerrstatus_ == 3) + { + /* If just tried and failed to reuse lookahead token after an + * error, discard it. */ + + if (yychar <= YYTokenType.EOF) + { + /* Return failure if at end of input. */ + if (yychar == YYTokenType.EOF) + return false; + } + else + yychar = yyempty_; + } + + /* Else will try to reuse lookahead token after shifting the error + * token. */ + label = YYERRLAB1; + break; + + /*---------------------------------------------------. + | errorlab -- error raised explicitly by YYERROR. | + `---------------------------------------------------*/ + case YYERROR: + + ]b4_locations_if([yyerrloc = yystack.locationAt (yylen - 1);])[ + /* Do not reclaim the symbols of the rule which action triggered + this YYERROR. */ + yystack.pop (yylen); + yylen = 0; + yystate = yystack.stateAt (0); + label = YYERRLAB1; + break; + + /*-------------------------------------------------------------. + | yyerrlab1 -- common code for both syntax error and YYERROR. | + `-------------------------------------------------------------*/ + case YYERRLAB1: + yyerrstatus_ = 3; /* Each real token shifted decrements this.xb */ + + for (;;) + { + yyn = yypact_[yystate]; + if (!yy_pact_value_is_default_ (yyn)) + { + yyn += yyterror_; + if (0 <= yyn && yyn <= yylast_ && yycheck_[yyn] == yyterror_) + { + yyn = yytable_[yyn]; + if (0 < yyn) + break; + } + } + + /* Pop the current state because it cannot handle the error token. */ + if (yystack.height == 1) + return false; + + ]b4_locations_if([yyerrloc = yystack.locationAt (0);])[ + yystack.pop (); + yystate = yystack.stateAt (0); + if (yydebug > 0) + yystack.print (yyDebugStream); + } + + ]b4_locations_if([ + /* Muck with the stack to setup for yylloc. */ + yystack.push (0, yy_semantic_null, yylloc); + yystack.push (0, yy_semantic_null, yyerrloc); + yyloc = yylloc_from_stack (yystack, 2); + yystack.pop (2);])[ + + /* Shift the error token. */ + yy_symbol_print ("Shifting", yystos_[yyn], + yylval]b4_locations_if([, yyloc])[); + + yystate = yyn; + yystack.push (yyn, yylval]b4_locations_if([, yyloc])[); + label = YYNEWSTATE; + break; + + /* Accept. */ + case YYACCEPT: + return true; + + /* Abort. */ + case YYABORT: + return false; + } + } + + // Generate an error message. + private final string yysyntax_error (int yystate, int tok) + { + if (errorVerbose) + { + /* There are many possibilities here to consider: + - Assume YYFAIL is not used. It's too flawed to consider. + See + + for details. YYERROR is fine as it does not invoke this + function. + - If this state is a consistent state with a default action, + then the only way this function was invoked is if the + default action is an error action. In that case, don't + check for expected tokens because there are none. + - The only way there can be no lookahead present (in tok) is + if this state is a consistent state with a default action. + Thus, detecting the absence of a lookahead is sufficient to + determine that there is no unexpected or expected token to + report. In that case, just report a simple "syntax error". + - Don't assume there isn't a lookahead just because this + state is a consistent state with a default action. There + might have been a previous inconsistent state, consistent + state with a non-default action, or user semantic action + that manipulated yychar. (However, yychar is currently out + of scope during semantic actions.) + - Of course, the expected token list depends on states to + have correct lookahead information, and it depends on the + parser not to perform extra reductions after fetching a + lookahead from the scanner and before detecting a syntax + error. Thus, state merging (from LALR or IELR) and default + reductions corrupt the expected token list. However, the + list is correct for canonical LR with one exception: it + will still contain any token that will not be accepted due + to an error action in a later state. + */ + if (tok != yyempty_) + { + // FIXME: This method of building the message is not compatible + // with internationalization. + string res = "syntax error, unexpected "; + res ~= yytnamerr_ (yytname_[tok]); + int yyn = yypact_[yystate]; + if (!yy_pact_value_is_default_ (yyn)) + { + /* Start YYX at -YYN if negative to avoid negative + indexes in YYCHECK. In other words, skip the first + -YYN actions for this state because they are default + actions. */ + int yyxbegin = yyn < 0 ? -yyn : 0; + /* Stay within bounds of both yycheck and yytname. */ + int yychecklim = yylast_ - yyn + 1; + int yyxend = yychecklim < yyntokens_ ? yychecklim : yyntokens_; + int count = 0; + for (int x = yyxbegin; x < yyxend; ++x) + if (yycheck_[x + yyn] == x && x != yyterror_ + && !yy_table_value_is_error_ (yytable_[x + yyn])) + ++count; + if (count < 5) + { + count = 0; + for (int x = yyxbegin; x < yyxend; ++x) + if (yycheck_[x + yyn] == x && x != yyterror_ + && !yy_table_value_is_error_ (yytable_[x + yyn])) + { + res ~= count++ == 0 ? ", expecting " : " or "; + res ~= yytnamerr_ (yytname_[x]); + } + } + } + return res; + } + } + + return "syntax error"; + } + + /** + * Whether the given yypact_ value indicates a defaulted state. + * @@param yyvalue the value to check + */ + private static bool yy_pact_value_is_default_ (int yyvalue) + { + return yyvalue == yypact_ninf_; + } + + /** + * Whether the given yytable_ value indicates a syntax error. + * @@param yyvalue the value to check + */ + private static bool yy_table_value_is_error_ (int yyvalue) + { + return yyvalue == yytable_ninf_; + } + + /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing + STATE-NUM. */ + private static immutable ]b4_int_type_for([b4_pact])[ yypact_ninf_ = ]b4_pact_ninf[; + private static immutable ]b4_int_type_for([b4_pact])[ yypact_[] = + ]m4_dquote([ + ]b4_pact[ + ])[; + + /* YYDEFACT[S] -- default reduction number in state S. Performed when + YYTABLE doesn't specify something else to do. Zero means the + default is an error. */ + private static immutable ]b4_int_type_for([b4_defact])[ yydefact_[] = + ]m4_dquote([ + ]b4_defact[ + ])[; + + /* YYPGOTO[NTERM-NUM]. */ + private static immutable ]b4_int_type_for([b4_pgoto])[ yypgoto_[] = + ]m4_dquote([ + ]b4_pgoto[ + ])[; + + /* YYDEFGOTO[NTERM-NUM]. */ + private static immutable ]b4_int_type_for([b4_defgoto])[ + yydefgoto_[] = + ]m4_dquote([ + ]b4_defgoto[ + ])[; + + /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If + positive, shift that token. If negative, reduce the rule which + number is the opposite. If YYTABLE_NINF_, syntax error. */ + private static immutable ]b4_int_type_for([b4_table])[ yytable_ninf_ = ]b4_table_ninf[; + private static immutable ]b4_int_type_for([b4_table])[ + yytable_[] = + ]m4_dquote([ + ]b4_table[ + ])[; + + /* YYCHECK. */ + private static immutable ]b4_int_type_for([b4_check])[ + yycheck_[] = + ]m4_dquote([ + ]b4_check[ + ])[; + + /* STOS_[STATE-NUM] -- The (internal number of the) accessing + symbol of state STATE-NUM. */ + private static immutable ]b4_int_type_for([b4_stos])[ + yystos_[] = + ]m4_dquote([ + ]b4_stos[ + ])[; + + /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal symbol number corresponding + to YYLEX-NUM. */ + private static immutable ]b4_int_type_for([b4_toknum])[ + yytoken_number_[] = + ]m4_dquote([ + ]b4_toknum[ + ])[; + + /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */ + private static immutable ]b4_int_type_for([b4_r1])[ + yyr1_[] = + ]m4_dquote([ + ]b4_r1[ + ])[; + + /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */ + private static immutable ]b4_int_type_for([b4_r2])[ + yyr2_[] = + ]m4_dquote([ + ]b4_r2[ + ])[; + + /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. + First, the terminals, then, starting at \a yyntokens_, nonterminals. */ + private static immutable string yytname_[] = + ]m4_dquote([ + ]b4_tname[ + ])[; + + /* YYRHS -- A `-1'-separated list of the rules' RHS. */ + private static immutable ]b4_int_type_for([b4_rhs])[ yyrhs_[] = + ]m4_dquote([ + ]b4_rhs[ + ])[; + + /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in + YYRHS. */ + private static immutable ]b4_int_type_for([b4_prhs])[ yyprhs_[] = + ]m4_dquote([ + ]b4_prhs[ + ])[; + + /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */ + private static immutable ]b4_int_type_for([b4_rline])[ yyrline_[] = + ]m4_dquote([ + ]b4_rline[ + ])[; + + // Report on the debug stream that the rule yyrule is going to be reduced. + private final void yy_reduce_print (int yyrule, ref YYStack yystack) + { + if (yydebug == 0) + return; + + int yylno = yyrline_[yyrule]; + int yynrhs = yyr2_[yyrule]; + /* Print the symbols being reduced, and their result. */ + yycdebug (std.string.format("Reducing stack by rule %d (line %d), ", + yyrule - 1,yylno)); + + /* The symbols being reduced. */ + for (int yyi = 0; yyi < yynrhs; yyi++) + yy_symbol_print (std.string.format(" $%d =",yyi + 1), + yyrhs_[yyprhs_[yyrule] + yyi], + ]b4_rhs_value(yynrhs, yyi + 1)b4_locations_if([, + b4_rhs_location(yynrhs, yyi + 1)])[); + } + + /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */ + private static immutable ]b4_int_type_for([b4_translate])[ yytranslate_table_[] = + ]m4_dquote([ + ]b4_translate[ + ])[; + + private static ]b4_int_type_for([b4_translate])[ yytranslate_ (int t) + { + if (t >= 0 && t <= yyuser_token_number_max_) + return yytranslate_table_[t]; + else + return yyundef_token_; + } + + private static immutable int yylast_ = ]b4_last[; + private static immutable int yynnts_ = ]b4_nterms_number[; + private static immutable int yyempty_ = -2; + private static immutable int yyfinal_ = ]b4_final_state_number[; + private static immutable int yyterror_ = 1; + private static immutable int yyerrcode_ = 256; + private static immutable int yyntokens_ = ]b4_tokens_number[; + + private static immutable int yyuser_token_number_max_ = ]b4_user_token_number_max[; + private static immutable int yyundef_token_ = ]b4_undef_token_number[; + +]/* User implementation code. */ +b4_percent_code_get[]dnl + +} + +b4_epilogue +m4_divert_pop(0)dnl diff --git a/data/local.mk b/data/local.mk index ea097aab..56cfebf1 100644 --- a/data/local.mk +++ b/data/local.mk @@ -32,6 +32,13 @@ dist_pkgdata_DATA = \ data/variant.hh \ data/yacc.c +# Experimental support for the D language. +dist_pkgdata_DATA += \ + data/README-D.txt \ + data/d-skel.m4 \ + data/d.m4 \ + data/lalr1.d + m4sugardir = $(pkgdatadir)/m4sugar dist_m4sugar_DATA = \ data/m4sugar/foreach.m4 \ -- cgit v1.2.1