Basic nodes

We have a few token types that are very easy to parse into AST nodes, as we can either skip over them, or they just map to an existing AST node.

Line Breaks

In the same way that our lexer skips line breaks, we want to do the same thing in our AST generator.

if (currentToken.type === TokenType.LineBreak) {
  currentIndex++
  return null
}

Increment our position, and then return nothing. We don't care about individual line breaks.

Literals / Strings

As we have these both as Tokens, and AST nodes, we are able to simply map them to their corresponding type.

if (currentToken.type === TokenType.Literal) {
  currentIndex++

  return {
    type: ASTNodeType.Literal,
    value: currentToken.value
  }
}

if (currentToken.type === TokenType.String) {
  currentIndex++

  return {
    type: ASTNodeType.String,
    value: currentToken.value
  }
}