Class: TRuby::ParserCombinator::ExpressionParser

Inherits:
Object
  • Object
show all
Includes:
TokenDSL
Defined in:
lib/t_ruby/parser_combinator/token/expression_parser.rb

Overview

Expression Parser - Parse expressions into IR nodes Uses Pratt parser (operator precedence parsing) for correct precedence

Constant Summary collapse

PRECEDENCE =

Operator precedence levels (higher = binds tighter)

{
  or_or: 1,      # ||
  and_and: 2,    # &&
  eq_eq: 3,      # ==
  bang_eq: 3,    # !=
  lt: 4,         # <
  gt: 4,         # >
  lt_eq: 4,      # <=
  gt_eq: 4,      # >=
  spaceship: 4,  # <=>
  pipe: 5,       # | (bitwise or)
  amp: 6,        # & (bitwise and)
  plus: 7,       # +
  minus: 7,      # -
  star: 8,       # *
  slash: 8,      # /
  percent: 8,    # %
  star_star: 9,  # ** (right-associative)
}.freeze
RIGHT_ASSOC =

Right-associative operators

Set.new([:star_star]).freeze
OPERATOR_SYMBOLS =

Token type to operator symbol mapping

{
  or_or: :"||",
  and_and: :"&&",
  eq_eq: :==,
  bang_eq: :!=,
  lt: :<,
  gt: :>,
  lt_eq: :<=,
  gt_eq: :>=,
  spaceship: :<=>,
  plus: :+,
  minus: :-,
  star: :*,
  slash: :/,
  percent: :%,
  star_star: :**,
  pipe: :|,
  amp: :&,
}.freeze

Instance Method Summary collapse

Methods included from TokenDSL

#keyword, #token

Instance Method Details

#parse_expression(tokens, position = 0) ⇒ Object



57
58
59
# File 'lib/t_ruby/parser_combinator/token/expression_parser.rb', line 57

def parse_expression(tokens, position = 0)
  parse_precedence(tokens, position, 0)
end