add exportMacro to list of valid grammar options. verified it works in grammar and -D on command line.

This commit is contained in:
parrt 2016-12-15 13:25:02 -08:00
parent d1b429d94c
commit 31726311a8
7 changed files with 35 additions and 16 deletions

View File

@ -79,10 +79,15 @@ This example assumes your grammar contains a parser rule named `key` for which t
There are a couple of things that only the C++ ANTLR target has to deal with. They are described here.
### Build Aspects
The code generation (by running the ANTLR4 jar) allows to specify 2 values you might find useful for better integration of the generated files into your application (both are optional):
The code generation (by running the ANTLR4 jar) allows to specify 2 values you
might find useful for better integration of the generated files into your
application (both are optional):
* A **namespace**: use the **`-package`** parameter to specify the namespace you want.
* An **export macro**: especially in VC++ extra work is required to export your classes from a DLL. This is usually accomplished by a macro that has different values depending on whether you are creating the DLL or import it. The ANTLR4 runtime itself also uses one for its classes:
* An **export macro**: especially in VC++ extra work is required to export your classes from a DLL.
This is usually accomplished by a macro that has different values depending on whether
you are creating the DLL or import it. The ANTLR4 runtime itself also uses one for its classes:
```c++
#ifdef ANTLR4CPP_EXPORTS
@ -95,16 +100,30 @@ The code generation (by running the ANTLR4 jar) allows to specify 2 values you m
#endif
#endif
```
Just like the `ANTLR4CPP_PUBLIC` macro here you can specify your own one for the generated classes using the **`-export-macro`** parameter.
In order to create a static lib in Visual Studio define the `ANTLR4CPP_STATIC` macro in addition to the project settings that must be set for a static library (if you compile the runtime yourself).
Just like the `ANTLR4CPP_PUBLIC` macro here you can specify your own one for
the generated classes using the **`-DexportMacro=...`** command-line parameter or
grammar option `options {exportMacro='...';}` in your grammar file.
For gcc and clang it is possible to use the `-fvisibility=hidden` setting to hide all symbols except those that are made default-visible (which has been defined for all public classes in the runtime).
In order to create a static lib in Visual Studio define the `ANTLR4CPP_STATIC` macro
in addition to the project settings that must be set for a static library
(if you compile the runtime yourself).
For gcc and clang it is possible to use the `-fvisibility=hidden` setting to
hide all symbols except those that are made default-visible (which has been
defined for all public classes in the runtime).
### Memory Management
Since C++ has no built-in memory management we need to take extra care. For that we rely mostly on smart pointers, which however might cause time penalties or memory side effects (like cyclic references) if not used with care. Currently however the memory household looks very stable. Generally, when you see a raw pointer in code consider this as being managed elsewehere. You should never try to manage such a pointer (delete, assign to smart pointer etc.).
Since C++ has no built-in memory management we need to take extra care.
For that we rely mostly on smart pointers, which however might cause time
penalties or memory side effects (like cyclic references) if not used with care.
Currently however the memory household looks very stable. Generally, when you
see a raw pointer in code consider this as being managed elsewehere. You
should never try to manage such a pointer (delete, assign to smart pointer etc.).
### Unicode Support
Encoding is mostly an input issue, i.e. when the lexer converts text input into lexer tokens. The parser is completely encoding unaware. However, lexer input in the grammar is defined by character ranges with either a single member (e.g. 'a' or [a] or [abc]), an explicit range (e.g. 'a'..'z' or [a-z]), the full Unicode range (for a wildcard) and the full Unicode range minus a sub range (for negated ranges, e.g. ~[a]). The explicit ranges (including single member ranges) are encoded in the serialized ATN by 16bit numbers, hence cannot reach beyond 0xFFFF (the Unicode BMP), while the implicit ranges can include any value (and hence support the full Unicode set, up to 0x10FFFF).
> An interesting side note here is that the Java target fully supports Unicode as well, despite the inherent limitations from the serialized ATN. That's possible because the Java String class represents characters beyond the BMP as surrogate pairs (two 16bit values) and even reads them as 2 separate input characters. To make this work a character range for an identifier in a grammar must include the surrogate pairs area (for a Java parser).
@ -114,6 +133,7 @@ The C++ target however always expects UTF-8 input (either in a string or via a w
The differences in handling characters beyond the BMP leads to a difference between Java and C++ lexers: the character offsets may not concur. This is because Java reads two 16bit values per Unicode char (if that falls into the surrogate area) while a C++ parser only reads one 32bit value. That usually doesn't have practical consequences, but might confuse people when comparing token positions.
### Named Actions
In order to help customizing the generated files there are a number of additional socalled **named actions**. These actions are tight to specific areas in the generated code and allow to add custom (target specific) code. All targets support these actions
* @parser::header

View File

@ -112,7 +112,6 @@ public class Tool {
public boolean gen_visitor = false;
public boolean gen_dependencies = false;
public String genPackage = null;
public String exportMacro = null; // C++ specific, need to allow setting a macro to set declspec for generated classes for VC++.
public Map<String, String> grammarOptions = null;
public boolean warnings_are_errors = false;
public boolean longMessages = false;
@ -129,7 +128,6 @@ public class Tool {
new Option("gen_visitor", "-visitor", "generate parse tree visitor"),
new Option("gen_visitor", "-no-visitor", "don't generate parse tree visitor (default)"),
new Option("genPackage", "-package", OptionArgType.STRING, "specify a package/namespace for the generated code"),
new Option("exportMacro", "-export-macro", OptionArgType.STRING, "C++ only, specify a macro for import/export of generated classes"),
new Option("gen_dependencies", "-depend", "generate file dependencies"),
new Option("", "-D<option>=value", "set/override a grammar-level option"),
new Option("warnings_are_errors", "-Werror", "treat warnings as errors"),

View File

@ -12,7 +12,7 @@ import java.util.Map;
public class LexerFile extends OutputFile {
public String genPackage; // from -package cmd-line
public String exportMacro; // from -export-macro cmd-line
public String exportMacro; // from -DexportMacro cmd-line
public boolean genListener; // from -listener cmd-line
public boolean genVisitor; // from -visitor cmd-line
@ModelElement public Lexer lexer;
@ -22,7 +22,7 @@ public class LexerFile extends OutputFile {
super(factory, fileName);
namedActions = buildNamedActions(factory.getGrammar());
genPackage = factory.getGrammar().tool.genPackage;
exportMacro = factory.getGrammar().tool.exportMacro;
exportMacro = factory.getGrammar().getOptionString("exportMacro");
genListener = factory.getGrammar().tool.gen_listener;
genVisitor = factory.getGrammar().tool.gen_visitor;
}

View File

@ -23,7 +23,7 @@ import java.util.Set;
*/
public class ListenerFile extends OutputFile {
public String genPackage; // from -package cmd-line
public String exportMacro; // from -export-macro cmd-line
public String exportMacro; // from -DexportMacro cmd-line
public String grammarName;
public String parserName;
/**
@ -62,6 +62,6 @@ public class ListenerFile extends OutputFile {
ActionAST ast = g.namedActions.get("header");
if ( ast!=null ) header = new Action(factory, ast);
genPackage = factory.getGrammar().tool.genPackage;
exportMacro = factory.getGrammar().tool.exportMacro;
exportMacro = factory.getGrammar().getOptionString("exportMacro");
}
}

View File

@ -16,7 +16,7 @@ import java.util.Map;
/** */
public class ParserFile extends OutputFile {
public String genPackage; // from -package cmd-line
public String exportMacro ; // from -export-macro cmd-line
public String exportMacro; // from -DexportMacro cmd-line
public boolean genListener; // from -listener cmd-line
public boolean genVisitor; // from -visitor cmd-line
@ModelElement public Parser parser;
@ -29,7 +29,7 @@ public class ParserFile extends OutputFile {
Grammar g = factory.getGrammar();
namedActions = buildNamedActions(factory.getGrammar());
genPackage = g.tool.genPackage;
exportMacro = g.tool.exportMacro;
exportMacro = factory.getGrammar().getOptionString("exportMacro");
// need the below members in the ST for Python, C++
genListener = g.tool.gen_listener;
genVisitor = g.tool.gen_visitor;

View File

@ -20,7 +20,7 @@ import java.util.Set;
public class VisitorFile extends OutputFile {
public String genPackage; // from -package cmd-line
public String exportMacro; // from -export-macro cmd-line
public String exportMacro; // from -DexportMacro cmd-line
public String grammarName;
public String parserName;
/**
@ -59,6 +59,6 @@ public class VisitorFile extends OutputFile {
ActionAST ast = g.namedActions.get("header");
if ( ast!=null ) header = new Action(factory, ast);
genPackage = factory.getGrammar().tool.genPackage;
exportMacro = factory.getGrammar().tool.exportMacro;
exportMacro = factory.getGrammar().getOptionString("exportMacro");
}
}

View File

@ -81,6 +81,7 @@ public class Grammar implements AttributeResolver {
parserOptions.add("TokenLabelType");
parserOptions.add("tokenVocab");
parserOptions.add("language");
parserOptions.add("exportMacro");
}
public static final Set<String> lexerOptions = parserOptions;