Merge pull request #2086 from ewanmellor/message-output

Improve the "gnu" message format
This commit is contained in:
Terence Parr 2017-11-04 10:49:37 -07:00 committed by GitHub
commit 21a38b3cea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 6 deletions

View File

@ -19,6 +19,16 @@ except you need to specify the language target, for example:
```
$ antlr4 -Dlanguage=Swift MyGrammar.g4
```
If you integrate this as a build step inside Xcode, then you should use the
"gnu" message format to have any error messages parsed by Xcode. You may
also want to use the `-o` option to put the autogenerated files in a
separate subdirectory.
```
antlr4 -Dlanguage=Swift -message-format gnu -o Autogen MyGrammar.g4
```
For a full list of antlr4 tool options, please visit the
[tool documentation page](tool-options.md).

View File

@ -31,9 +31,9 @@ This file contains the actual layout of the messages emitted by ANTLR.
This file contains the format that mimicks GCC output.
*/
location(file, line, column) ::= "<file>:<line>:"
location(file, line, column) ::= "<file>:<line>:<column>:"
message(id, text) ::= "<text> (<id>)"
message(id, text) ::= "<text> [error <id>]"
report(location, message, type) ::= "<location> <type>: <message>"

View File

@ -60,11 +60,20 @@ public class ErrorManager {
locationValid = true;
}
if (msg.fileName != null) {
File f = new File(msg.fileName);
// Don't show path to file in messages; too long.
String displayFileName = msg.fileName;
if ( f.exists() ) {
displayFileName = f.getName();
if (format.equals("antlr")) {
// Don't show path to file in messages in ANTLR format;
// they're too long.
File f = new File(msg.fileName);
if ( f.exists() ) {
displayFileName = f.getName();
}
}
else {
// For other message formats, use the full filename in the
// message. This assumes that these formats are intended to
// be parsed by IDEs, and so they need the full path to
// resolve correctly.
}
locationST.add("file", displayFileName);
locationValid = true;