Skip to content
Discussion options

You must be logged in to vote

You can do something like this (using the HelloWorld grammar as an example):

checkPersonPosition(greeting: Greeting, acceptor: ValidationAcceptor): void {
  const greetingOffset = greeting.$cstNode?.offset ?? 0;
  const personOffset = greeting.person.ref?.$cstNode?.offset ?? 0;
  if (person > greetingOffset) {
    acceptor('error', 'Person must be declared before greeting them', {
      node: greeting,
      property: 'person'
    });
  }
}

Depending on the complexity of your language, you might need to to more AST analysis to determine whether the variable has been declared yet. However, this simple comparison should help you along quite well.

Replies: 2 comments 3 replies

Comment options

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

Comment options

You must be logged in to vote
2 replies
@msujew
Comment options

Answer selected by msujew
@cdietrich-everest
Comment options

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