Syntactic Abstraction Code
Syntactic abstraction is a mechanism to support the Fortress language growth. The design of the system is described in our FOOL'09 paper. This page includes a list of the files implementing the syntactic abstraction mechanism with brief comments.
Parser
- Fortress grammar for defining new syntax is defined in: trunk/ProjectFortress/src/com/sun/fortress/parser/Syntax.rats
- Template parser is extended to add template nodes and ellipses nodes as follows:
- trunk/ProjectFortress/src/com/sun/fortress/parser/templateparser/Symbol.rats:
transient void DoubleStar = "**";
- trunk/ProjectFortress/src/com/sun/fortress/parser/templateparser/Identifier.rats:
Id Id += NameGap / IdGap / <FIRST> ... ; List<Id> BindIdOrBindIdTuple += BindIdOrBindIdTupleGap / <FIRST> ... ; - trunk/ProjectFortress/src/com/sun/fortress/parser/templateparser/Literal.rats:
Expr LiteralExpr += ExprGap / LooseJuxtGap / SimpleExprGap / PrimaryGap / LiteralExprGap / <VOID> ...; Expr VoidLiteralExpr += VoidLiteralExprGap / <FIRST> ...; IntLiteralExpr IntLiteralExpr += NumberLiteralExprGap / IntLiteralExprGap / <FIRST> ...; CharLiteralExpr CharLiteralExpr += CharLiteralExprGap / <FIRST> ...; StringLiteralExpr StringLiteralExpr += StringLiteralExprGap / <FIRST> ... ; - trunk/ProjectFortress/src/com/sun/fortress/parser/templateparser/Expression.rats
Expr ExprFront += ExprGap / LooseJuxtGap / FnExprGap / <Fn> ... ; Action<Expr> ExprTail += EllipsesExpr / <As> ...; constant inline Action<Expr> EllipsesExpr = w DoubleStar { yyValue = new Action<Expr>() { public Expr run(Expr base) { return new _EllipsesExpr(NodeFactory.makeExprInfo(createSpan(yyStart,yyCount)), base); }}; }; - trunk/ProjectFortress/src/com/sun/fortress/parser/templateparser/NoNewlineExpr.rats:
Action<Expr> ExprTail += NoNewlineEllipsesExpr / <As> ...; constant inline Action<Expr> NoNewlineEllipsesExpr = s DoubleStar { yyValue = new Action<Expr>() { public Expr run(Expr base) { return new _EllipsesExpr(NodeFactory.makeExprInfo(createSpan(yyStart,yyCount)), base); }}; }; - trunk/ProjectFortress/src/com/sun/fortress/parser/templateparser/Gaps.rats
- trunk/ProjectFortress/src/com/sun/fortress/parser/templateparser/Symbol.rats:
Entry Points
There are two entry points to the syntactic abstraction code:
- Parser.macroParse by GraphRepository.syntaxExpand: If a given component imports APIs defining new syntax, use parseWithGrammars. Otherwise, use parseFileConvertExn.
- GrammarRewriter.rewriteApis by GrammarPhase.execute: If any API defines new syntax, rewrite them.
Parsing Programs with New Syntax (parseWithGrammars)
- ParserMaker.parserForComponent: Create a parser from the grammars.
- GrammarComposer.pegForComponent: Composes grammars together into a flat PEG.
- ParserMaker.makeParser: Compile a parser given a set of nonterminals and productions.
- Turn PEG into a single new module using ComposingSyntaxDefTranslator. Use ArrayUnpacker to handle ellipses nodes.
- RatsParserGenerator.generateParser: Given a set of Rats! modules, generate a new Fortress parser extended with the modifications in the given modules.
- RatsUtil.getParserObject: Create a Java parser by invoking Rats! and parse the original component with that parser.
- Transform.transform: Macro expander. Traverse a given AST node, replacing transformation nodes with the results of their transformer executions, and return a core Fortress AST.
- SyntaxEnvironment: Environment used for hygienic transformation
Grammar Rewriting (GrammarRewriter.rewriteApis)
- ItemDisambiguator: Disambiguate item symbols and rewrite to either nonterminal, keyword or token symbol.
- Disambiguate nonterminal parameters. (Not yet done)
- WhitespaceElimination: Remove whitespace where indicated by no-whitespace symbols.
- EscapeRewriter: Rewrite escaped symbols.
- ExtensionDesugarer: Desugar extensions: fill in unmentioned imported grammars and collect multiple extensions of same nonterminal together.
- RewriteTransformerNames: Rewrite transformer names.
- TemplateParser: Parse pretemplates and replace with real templates.
- TemplateVarRewriter: Rewrite all occurrences of variables in a given template to occurrences of gaps using BaseTypeCollector.
Other Code
Utilities
- Ellipses environment
- Gap environment
- Nonterminal environment using FortressTypeToJavaType
- Environment factories
- Variable collector
ASTGen CustomGenerators
- Ellipses nodes: EllipsesNode, EllipsesNodeCreator
- Template nodes: TemplateGapClass, TemplateGapNodeCreator, TemplateVisitorGenerator, TemplateDepthFirstVisitorGenerator, TemplateDepthFirstVoidVisitorGenerator
- Transformation nodes: TransformationNode, TransformationNodeCreator
- FortressAst libraries: FortressAstGenerator
- SingleSpanConstructorGenerator
Compiler
- Indices: GrammarIndex, NonterminalDefIndex, NonterminalExtendIndex, NonterminalIndex
- Nonterminal disambiguator: NonterminalDisambiguator, NonterminalEnv, NonterminalNameDisambiguator

