Class: Faust2Ruby::Lexer

Inherits:
Object
  • Object
show all
Defined in:
lib/faust2ruby/lexer.rb

Overview

Lexer for Faust DSP source code. Uses StringScanner for efficient tokenization.

Defined Under Namespace

Classes: Token

Constant Summary collapse

KEYWORDS =

Keywords in Faust

%w[
  import declare process with letrec where
  par seq sum prod
  case of
  environment library component
  inputs outputs
].freeze
MULTI_CHAR_OPS =

Multi-character operators (must check before single-char)

{
  "<:" => :SPLIT,
  ":>" => :MERGE,
  "==" => :EQ,
  "!=" => :NEQ,
  "<=" => :LE,
  ">=" => :GE,
  "<<" => :LSHIFT,
  ">>" => :RSHIFT,
  "=>" => :ARROW,
}.freeze
SINGLE_CHAR_OPS =

Single-character operators and punctuation

{
  ":" => :SEQ,
  "," => :PAR,
  "~" => :REC,
  "+" => :ADD,
  "-" => :SUB,
  "*" => :MUL,
  "/" => :DIV,
  "%" => :MOD,
  "^" => :POW,
  "@" => :DELAY,
  "'" => :PRIME,
  "=" => :DEF,
  ";" => :ENDDEF,
  "(" => :LPAREN,
  ")" => :RPAREN,
  "{" => :LBRACE,
  "}" => :RBRACE,
  "[" => :LBRACKET,
  "]" => :RBRACKET,
  "<" => :LT,
  ">" => :GT,
  "&" => :AND,
  "|" => :OR,
  "!" => :CUT,
  "_" => :WIRE,
  "." => :DOT,
  "\\" => :LAMBDA,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Lexer

Returns a new instance of Lexer.



67
68
69
70
71
72
73
74
# File 'lib/faust2ruby/lexer.rb', line 67

def initialize(source)
  @source = source
  @scanner = StringScanner.new(source)
  @tokens = []
  @errors = []
  @line = 1
  @line_start = 0
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



65
66
67
# File 'lib/faust2ruby/lexer.rb', line 65

def errors
  @errors
end

#tokensObject (readonly)

Returns the value of attribute tokens.



65
66
67
# File 'lib/faust2ruby/lexer.rb', line 65

def tokens
  @tokens
end

Instance Method Details

#tokenizeObject



76
77
78
79
80
81
82
83
# File 'lib/faust2ruby/lexer.rb', line 76

def tokenize
  until @scanner.eos?
    token = next_token
    @tokens << token if token
  end
  @tokens << Token.new(type: :EOF, value: nil, line: @line, column: current_column)
  @tokens
end