Skip to content

Can I create a visitor class without having an instance of the parser ? #1884

Answered by dhowe
dhowe asked this question in Q&A
Discussion options

You must be logged in to vote

Seems its as simple as this (unless I'm missing something):

class BaseVisitor {
  visit(cstNode, param) {
    if (Array.isArray(cstNode)) {
      cstNode = cstNode[0];
    }
    if (typeof cstNode === 'undefined') {
      return undefined;
    }
    return this[cstNode.name](cstNode.children, param);
  }
  validateVisitor() {
    // no-op
  }
}

Replies: 4 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@bd82
Comment options

bd82 Nov 16, 2022
Maintainer

Comment options

You must be logged in to vote
1 reply
@bd82
Comment options

bd82 Nov 17, 2022
Maintainer

Answer selected by dhowe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants