Search code examples
typescriptplsqlantlr4

Antlr4 and TypeScript: type error when using grammar specific lexer and parser


When trying to use the PlSql in a basic example as described by the documentation TypeScript reports a type error when using the grammar specific PlSqlLexer and PlSqlParser.

This is how the grammar is generated:

# clean
rm -rf grammar
mkdir grammar

# download grammar
curl --output grammar/PlSqlLexer.g4 https://raw.githubusercontent.com/antlr/grammars-v4/master/sql/plsql/PlSqlLexer.g4
curl --output grammar/PlSqlParser.g4 https://raw.githubusercontent.com/antlr/grammars-v4/master/sql/plsql/PlSqlParser.g4

### convert grammar for Typescript
antlr4 -Dlanguage=TypeScript grammar/PlSqlLexer.g4
antlr4 -Dlanguage=TypeScript grammar/PlSqlParser.g4

This is the example code:

import antlr4 from 'antlr4';
import PlSqlLexer from './grammar/PlSqlLexer';
import PlSqlParser from './grammar/PlSqlParser';

const input = 'select * from dual';
const chars = new antlr4.CharStream(input);
const lexer = new PlSqlLexer(chars);
const tokens = new antlr4.CommonTokenStream(lexer); /* type error 1 */
const parser = new PlSqlParser(tokens);
const tree = parser.sql_script();
console.log(tree.toStringTree(null, parser)); /* type error 2 */

type error 1:

Argument of type 'PlSqlLexer' is not assignable to parameter of type 'Lexer'. Type 'PlSqlLexer' is missing the following properties from type 'Lexer': _input, _interp, text, line, and 18 more.

type error 2:

Argument of type 'PlSqlParser' is not assignable to parameter of type 'Parser'. Type 'PlSqlParser' is missing the following properties from type 'Parser': _input, _ctx, _interp, _errHandler, and 25 more.


Solution

  • The problem was resolved with the latest revision of the PlSql grammar and now the following example does work as expected:

    import * as antlr4 from 'antlr4';
    import PlSqlLexer from './grammar/PlSqlLexer';
    import PlSqlParser from './grammar/PlSqlParser';
    
    const chars = new antlr4.CharStream('select * from dual;');
    const lexer = new PlSqlLexer(chars);
    const tokens = new antlr4.CommonTokenStream(lexer);
    const parser = new PlSqlParser(tokens);
    const tree = parser.sql_script();
    console.log(tree.toStringTree(null, parser));
    

    after generating the grammar with this script:

    #!/bin/sh
    
    # clean
    rm -rf grammar
    mkdir grammar
    
    # download grammar
    curl --output grammar/PlSqlLexer.g4 https://raw.githubusercontent.com/antlr/grammars-v4/master/sql/plsql/PlSqlLexer.g4
    curl --output grammar/PlSqlParser.g4 https://raw.githubusercontent.com/antlr/grammars-v4/master/sql/plsql/PlSqlParser.g4
    
    ### convert grammar for Typescript
    antlr4 -Dlanguage=TypeScript grammar/PlSqlLexer.g4
    antlr4 -Dlanguage=TypeScript grammar/PlSqlParser.g4
    
    # download Typescript support files
    curl --output grammar/PlSqlLexerBase.ts https://raw.githubusercontent.com/antlr/grammars-v4/master/sql/plsql/TypeScript/PlSqlLexerBase.ts
    curl --output grammar/PlSqlParserBase.ts https://raw.githubusercontent.com/antlr/grammars-v4/master/sql/plsql/TypeScript/PlSqlParserBase.ts