class Drizzle::Parser

Overview

A Parser reads in the tokens generated by a Lexer and constructs an Abstract Syntax Tree (AST) built from what it finds.

Defined in:

drizzle/parser.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(lexer : Lexer) #

[View source]

Instance Method Detail

def current : Drizzle::Token #

The token that is currently being inspected by the parser


[View source]
def eat?(expected_type : TokenType) : Bool #

"Eat" the @peek token if the expected type matches the type of @peek

Eating a token involves passing in the type of token that @peek is expected to be. If it is the expected type, this method will advance the parser to the next token


[View source]
def eat_error(expected_type : TokenType) #

Add an error when #eat? gets an incorrect token type


[View source]
def errors : Array(String) #

Maintain an array of errors that are generated during the parsing step


[View source]
def initialise_parsers #

Initialize parser methods for the parser to use when parsing expressions


[View source]
def next_token #

Update the @current and @peek pointers contained in the Parser instance to the next token generated from the Lexer


[View source]
def parse_block_statement : AST::BlockStatement? #

Attempt to parse a block statement found at the current token, returning the node if possible, or nil if not


[View source]
def parse_boolean_literal : AST::Expression #

Parse a boolean found at the current token boolean


[View source]
def parse_call_arguments : Array(AST::Expression) #

Parse an argument list for a call expression (expression*)


[View source]
def parse_call_expression(left : AST::Expression) : AST::Expression #

Parse a call expression found at the current token identifier argument_list


[View source]
def parse_expression(precedence : Precedence) : AST::Expression? #

Attempt to parse an expression. This method is given a current precedence to compare against when necessary.


[View source]
def parse_expression_statement : AST::ExpressionStatement? #

Attempt to parse an expression statement found at the current token, returning the node if possible, or nil if not


[View source]
def parse_function : AST::Function? #

Attempt to parse a function found at the current token, returning the node if possible, or nil if not def name((name: type)*) -> type block


[View source]
def parse_function_parameters : Array(AST::TypedIdentifier)? #

Parse the parameter list for a function and return an array of AST::TypedIdentifier instances representing the parameters


[View source]
def parse_grouped_expression : AST::Expression? #

Parse a grouped expression found at the current token (expression)


[View source]
def parse_identifier : AST::Expression #

Parse an identifier found at the current token name


[View source]
def parse_if_statement(check_alternatives : Bool = true) : AST::IfStatement? #

Attempt to parse an 'if' statement found at the current token, returning the node if possible, or nil if not.

The check_alternatives flag states whether the current call should check for elsif and else tokens. We can use this to parse elsif blocks recursively.


[View source]
def parse_infix_expression(left : AST::Expression) : AST::Expression #

Parse an infix expression found at the current token expression operator expression


[View source]
def parse_integer_literal : AST::Expression #

Parse an integer found at the current token integer


[View source]
def parse_let_statement : AST::Let? #

Attempt to parse a let statement found at the current token, returning the node if possible, or nil if not


[View source]
def parse_prefix_expression : AST::Expression #

Parse a prefix expression found at the current token operator expression


[View source]
def parse_program : AST::Program #

Parse a program and build a program node from it, and return it


[View source]
def parse_return_statement : AST::Return? #

Attempt to parse a return statement found at the current token, returning the node if possible, or nil if not


[View source]
def parse_statement : AST::Statement? #

Parse a statement found at @current and return it, or nil if no statement can be parsed


[View source]
def parse_typed_identifier : AST::TypedIdentifier? #

Parse a typed identifier found at the current token name: datatype


[View source]
def peek : Drizzle::Token #

A pointer to the next token coming up in the stream. This is used to help guide what kind of node will be built from @current


[View source]
def register_infix(token_type : TokenType, func : InfixParser) #

Register a TokenType with a parser function that is run when the TokenType is discovered in a spot for infix notation


[View source]
def register_prefix(token_type : TokenType, func : PrefixParser) #

Register a TokenType with a parser function that is run when the TokenType is discovered in a spot for prefix notation


[View source]