TypeScript antlr4ts监听器类型抛出错误

时间:2018-03-15 12:15:27

标签: angularjs typescript antlr4

我正在尝试在角项目中使用antlr4解析器。

有一个dataservice类,其中调用的函数如下所示

parseRule() {
  const ruleString = ' STRING TO PARSE';

  const inputStream = new ANTLRInputStream(ruleString);
  const lexObject = new lexer.scopeLexer(inputStream);
  const tokenStream = new CommonTokenStream(lexObject);
  const parseObject = new parser.scopeParser(tokenStream);
  const result = parseObject.file();
  const evaluator = new ScopeEvaluator();
  const walker = new ParseTreeWalker();

  walker.walk(evaluator, result);
  console.log(' result :', result.text);
}

客户监听器正在使用一个enterNonGraphScope方法实现导出的接口。

import { scopeListener } from './antlrscope/scopeListener';
import { NonGraphScopeContext, NamesetSimpleContext } from './antlrscope/scopeParser';

export class ScopeEvaluator implements scopeListener {

  constructor() {
    console.log('constructed the asdfasf');
  }

  enterNonGraphScope = function (ctx: NonGraphScopeContext) {
    console.log('Tis ', ctx.text);
  };

}

为了说明界面

,从scopeListener.ts中粘贴一些片段
export interface scopeListener extends ParseTreeListener {

/**
 * Enter a parse tree produced by the `NonGraphScope`
 * labeled alternative in `scopeParser.scope`.
 * @param ctx the parse tree
*/
enterNonGraphScope?: (ctx: NonGraphScopeContext) => void;

当我运行角度ng服务时,它正在编译打字稿代码。 我收到了错误

src / app / rule-parser.service.ts(31,17)中的错误:错误TS2559:键入' ScopeEvaluator'没有与“ParseTreeListener'”类型相同的属性。

下面是打字稿中生成的监听器。 (删除了内容和antlr生成的评论)

export interface scopeListener extends ParseTreeListener {
    enterNonGraphScope?: (ctx: NonGraphScopeContext) => void;
    exitNonGraphScope?: (ctx: NonGraphScopeContext) => void;
    enterBlockScope?: (ctx: BlockScopeContext) => void;
    exitBlockScope?: (ctx: BlockScopeContext) => void;
    enterNamesetSimple?: (ctx: NamesetSimpleContext) => void;
    exitNamesetSimple?: (ctx: NamesetSimpleContext) => void;
    enterGrainExpression?: (ctx: GrainExpressionContext) => void;
    exitGrainExpression?: (ctx: GrainExpressionContext) => void;
    enterGrainSimple?: (ctx: GrainSimpleContext) => void;
    exitGrainSimple?: (ctx: GrainSimpleContext) => void;
    enterNamesetExpression?: (ctx: NamesetExpressionContext) => void;
}

这似乎与打字稿解释或打字有关。 我是javascript / typescript的新手。你能帮帮我吗?

webpack /生成的javascript代码运行正常,但此错误会阻止生成构建。

非常感谢!

-Vinayak

1 个答案:

答案 0 :(得分:0)

我对此进行了一些谷歌搜索,并发现这与弱类型处理更改有关。有关以下链接的更多详情。

https://blogs.msdn.microsoft.com/typescript/2017/06/12/announcing-typescript-2-4-rc/

基于此另一个stackoverflow讨论 After upgrading TypeScript, Angular controller registration now fails to compile

以下修改实现接口的类工作。 选项1更好,因为它只是一种类型断言。

选项1

    parseRule() {
      const ruleString = ' Scope: (Dim1.Attr1 * &Namset1);' +
        ' Measure.[asdfa] = ddd ;' +
        'end scOPe;';

      const inputStream = new ANTLRInputStream(ruleString);
      const lexObject = new lexer.scopeLexer(inputStream);
      const tokenStream = new CommonTokenStream(lexObject);
      const parseObject = new parser.scopeParser(tokenStream);
      const result = parseObject.file();
      const evaluator = new ScopeEvaluator();
      const walker = new ParseTreeWalker();
      console.log(' type of ' , evaluator);
      walker.walk(evaluator as ParseTreeListener, result);
        // Parse and execute the code.
        console.log(' p :', parseObject);
      console.log(' result :', result.text);
    }

选项2 - 涉及重新声明内容?

            import { scopeListener } from './antlrscope/scopeListener';
            import { NonGraphScopeContext, NamesetSimpleContext } from './antlrscope/scopeParser';
            import { ParseTreeListener } from 'antlr4ts/tree/ParseTreeListener';


            export class ScopeEvaluator implements scopeListener {

                visitTerminal = () => { };
                visitErrorNode = () => { };
                enterEveryRule = () => { };
                exitEveryRule = () => { };
                enterNonGraphScope = function (ctx: NonGraphScopeContext) {
                    console.log('Tis ', ctx.text);
                };
            }

如果您认为这不是正确的做法,请发表评论。我对打字稿的理解并不完整。