0

I have the following code taking the input of my input file:

var inputStream = new AntlrInputStream(File.ReadAllText(fileName));
var lexer = new LegitusLexer(inputStream);
var commonTokenStream = new CommonTokenStream(lexer);
var parser = new LegitusParser(commonTokenStream);
parser.AddErrorListener(this);

var context = parser.program();
var visitor = new LegitusVisitor(_io.GetDefaultMethods(), _io.GetDefaultVariables())
{
    Logger = _logger
};
visitor.Visit(context);

But when I call parser.program(), my program runs as it should. However, I need a way to validate that the input file is syntactically correct, so that users can verify without having to run the scripts (which run against a special machine).

Does Antlr4csharp support this easily?

Tylor Pater
  • 95
  • 1
  • 7
  • When you say syntactically correct, what is it you are looking for exactly? What is the use case, and what is the desired, vs actual outcome? If you want to test something fails, I would suggest create something that fails and run your tests – Simon Price Apr 25 '22 at 20:01
  • Without running *what*? If you don't want to run the input file, just delete everything after `var context = parser.program();`. Am I missing something? – Sweeper Apr 25 '22 at 20:06
  • Instead of syntactically correct, do you perhaps mean semantically correct? As pointed out by Sweeper, when `parser.program()` does not report/produce any errors, the input was syntactically correct. – Bart Kiers Apr 25 '22 at 20:30

1 Answers1

0

The Antlr tool can be used to lint the source.

The only difference from a 'standard' tool run is that no output files are generated -- the warnings/errors will be the same.

For example (Java; string source content w/manually applied file 'name'):

    Tool tool = new Tool();
    tool.removeListeners();
    tool.addListener(new YourLintErrorReporter());

    ANTLRStringStream in = new ANTLRStringStream(content);
    GrammarRootAST ast = tool.parse(name, in);
    Grammar g = tool.createGrammar(ast);
    g.fileName = name; // to ensure all err msgs identify the file by name
    tool.process(g, false); // false -> lint: don't gencode

The CS implementation is equivalent.

GRosenberg
  • 5,843
  • 2
  • 19
  • 23