ANTLR-Mode Overview Details »Project »News & Download

Details of ANTLR-Mode

  1. Menus and Key-Bindings
  2. Syntax Highlighting & Indentation
  3. Makefile Rule Creation
  4. Insertion of Options

Menus and Key-Bindings

This package defines the following menu entries and key-bindings

Syntax Highlighting & Indentation

Syntax highlighting & indentation can be best demonstrated by a small example. It shows how the first 35 lines from examples/java/exprAST/expr.g in the distribution of ANTLR-2.7.1 would look like after you have used the automatic indentation (see item Indent Region in the major mode menu Antlr). Of course, I assume that you have turned on font-lock-mode.

{
import java.io.*;
}

class ExprParser extends Parser;

options {
    codeGenMakeSwitchThreshold = 3;
    codeGenBitsetTestThreshold = 4;
    buildAST=true;
    ASTLabelType = "antlr.CommonAST"; // change default of "AST"
}

expr : assignExpr EOF! ;

assignExpr
    :   addExpr
        (
            ASSIGN^
            assignExpr 
        )?
    ;

addExpr
    :   multExpr 
        (
            pm:PLUS_MINUS^
            me:multExpr
            exception catch [ RecognitionException ex ] 
            { 
                System.out.println("Caught error in addExpr");
                reportError(ex.toString()); 
            }
        )*
    ;

Syntax highlighting emphasizes comments, ANTLR's keywords, syntactic symbols, rule and token definitions and references, characters and strings. The highlighting of the code in actions depends on ANTLR's language option: we use the usual syntax highlighting rules for that language.

The indentation is based on ANTLR's (intended) indentation style: we calculate the paren/brace/bracket depth-level and determine the position inside the rule (header, body, exception part). For the indentation of code in actions we use the indentation engine of package cc-mode.

Technical detail: documentation of variable antlr-font-lock-maximum-decoration, function antlr-indent-line and variable antlr-indent-style.

Makefile Rule Creation

The creation of Makefile rules can also best be shown by an example. It shows the Makefile rules ANTLR-Mode creates for the grammar files in directory examples/cpp/tinyc/ after you have copied file examples/cpp/inherit.tinyc/subc.g into that directory (to demonstrate grammar inheritance).

GENS1 = \
	TinyCLexer.cpp TinyCLexer.hpp \
	TinyCTokenTypes.txt TinyCTokenTypes.hpp
$(GENS1): lexer.g
	$(ANTLR) $<

GENS2 = expandedsubc.g \
	MyCParser.cpp MyCParser.hpp \
	MyCParserTokenTypes.txt MyCParserTokenTypes.hpp
$(GENS2): subc.g tinyc.g TinyCParserTokenTypes.txt
	$(ANTLR) -glib tinyc.g $<

GENS3 = \
	TinyCParser.cpp TinyCParser.hpp \
	TinyCParserTokenTypes.txt TinyCParserTokenTypes.hpp
$(GENS3): tinyc.g TinyCTokenTypes.txt
	$(ANTLR) $<

GENS = $(GENS1) $(GENS2) $(GENS3)

Technical detail: documentation of command antlr-show-makefile-rules.

Insertion of Options

Can can use submenus of the major mode menu Antlr or the key sequence C-c C-o to insert or change file, grammar, rule or subrule options.

Technical detail: documentation of command antlr-insert-option.

Christoph Wedler, 13 Nov 2014