项目作者: dozmus

项目描述 :
A macro pre-processor implementation for ANTLR4.
高级语言: ANTLR
项目地址: git://github.com/dozmus/antlr4-macro.git
创建时间: 2016-08-01T20:08:40Z
项目社区:https://github.com/dozmus/antlr4-macro

开源协议:MIT License

下载


antlr4-macro

A macro pre-processor for ANTLR4.

Usage

You should add pre-build event(s) in your IDE of choice to run the
antlr4-macro artifact, and thus output standard ANTLR4 grammar files.

You can specify either target file, i.e. -i my-grammar.mg4 or the
entire working directory, i.e. -i ..
You may also enable recursive traversal using -r.
When traversing the directory, only files with extension mg4 will be
processed.

More usage information available at java -jar antlr4-macro.jar -help

What is it?

C-like macros for your ANTLR4 grammar code.
e.g. #MY_MACRO: 'my macro'; and #MACRO2(A, B): A ' ' B;.

Note: The scope of a macro rule is the entire file it is in.

In-built macro rules

Name Arguments Output (ANTLR4 grammar code)
lower Anything Its entire argument in lowercase.
upper Anything Its entire argument in uppercase.
list Anything Anything (',' Anything)*
list Anything, Delimiter Anything (Delimeter Anything)*

Note: You can redefine an in-built macro, however you cannot redefine your own macros.
This will result in the RedefinedMacroRuleException being thrown.

Example

Input:

  1. grammar HelloWorld;
  2. // my macro definitions
  3. #HELLO: 'Hello';
  4. #WORLD: 'World';
  5. // parser rules
  6. helloWorldList1: list(HELLOWORLD1);
  7. helloWorldList2: list(HELLOWORLD2, ' ');
  8. // lexer rules
  9. HELLOWORLD1: #HELLO #WORLD;
  10. HELLOWORLD2: lower('Hello') upper('World');

Rough output:

  1. grammar HelloWorld;
  2. // parser rules
  3. helloWorldList1: HELLOWORLD1 (',' HELLOWORLD1)*;
  4. helloWorldList2: HELLOWORLD2 (' ' HELLOWORLD2)*;
  5. // lexer rules
  6. HELLOWORLD1: 'Hello' 'World';
  7. HELLOWORLD2: 'hello' 'WORLD';

License

This project is licensed under the MIT license.