//=========================================================================== // // Semantics to handle typedefs in PEG for C // compatible with Mouse 1.1 - 1.5. // //--------------------------------------------------------------------------- // // Copyright (C) 2007, 2009, 2010 by Roman R Redziejowski (www.romanredz.se). // // The author gives unlimited permission to copy and distribute // this file, with or without modifications, as long as this notice // is preserved, and any changes are properly documented. // // This file 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 comment in 'C.peg' for overview of typedef handling. // //--------------------------------------------------------------------------- // // Change log // 2009-07-13 Posted on Internet. // 2009-08-16 Updated for Mouse 1.1. // //=========================================================================== import java.util.Vector; import java.util.HashSet; class C extends mouse.runtime.SemanticsBase { //======================================================================= // // Typedef table // //======================================================================= HashSet typedefs; //------------------------------------------------------------------- // Initialization. // This method is called before the processing of each input file. //------------------------------------------------------------------- public void init() { typedefs = new HashSet(); } //======================================================================= // // Semantic actions // //======================================================================= //------------------------------------------------------------------- // Declaration = DeclarationSpecifiers InitDeclaratorList? SEMI // 0 1 1/2 //------------------------------------------------------------------- void Declaration() { // If InitDeclaratorList is present and DeclarationSpecifiers // contain "typedef", copy all Identifiers delivered // by InitDeclaratorList into typedefs table. if (rhsSize()==1 || rhs(0).get()==null) return; Vector iList = (Vector)(rhs(1).get()); for (String s: iList) typedefs.add(s); } //------------------------------------------------------------------- // DeclarationSpecifiers = (StorageClassSpecifier / TypeQualifier / // FunctionSpecifier)* TypedefName (StorageClassSpecifier / // TypeQualifier / FunctionSpecifier)* // DeclarationSpecifiers = (StorageClassSpecifier / TypeSpecifier / // TypeQualifier / FunctionSpecifier)+ //------------------------------------------------------------------- void DeclarationSpecifiers() { // This semantic action is called by both alternatives // of DeclarationSpecifiers. // Scan all Specifiers and return semantic value "typedef" // if any of them is "typedef". lhs().put(null); for (int i=0;i=7 && rhs(i).text().substring(0,7).equals("typedef")) { lhs().put("typedef"); return; } } //------------------------------------------------------------------- // InitDeclaratorList = InitDeclarator (COMMA InitDeclarator)* // 0 1,3,.. 2,4,.. //------------------------------------------------------------------- void InitDeclaratorList() { // Build Vector of Identifiers delivered by InitDeclarators // and return it as semantic value. Vector iList = new Vector(); for (int i=0;i