Module: Jade::Lexer

Extended by:
Lexer
Included in:
Lexer
Defined in:
lib/jade/lexer.rb

Constant Summary collapse

KEYWORDS =
::Set[
  'def',
  'type',
  'module',
  'import',
  'exposing',
  'if',
  'then',
  'else',
  'case',
  'in',
  'end',
  'as',
  'uses',
  'with',
  'struct',
  'interface',
  'implements',
  'extends',
].freeze
SYMBOLS =
{
  '->' => :arrow,
  '('  => :lparen,
  ')'  => :rparen,
  ':'  => :colon,
  ','  => :comma,
  '{'  => :lbrace,
  '}'  => :rbrace,
  '['  => :lbrack,
  ']'  => :rbrack,
  '..' => :dotdot,
  '.'  => :dot,
  '@'  => :at,

  # arithmetic
  '+'  => :plus,
  '-'  => :minus,
  '*'  => :star,
  '/'  => :slash,
  '|'  => :pipe,

  # comparison
  '==' => :eq,
  '!=' => :not_eq,
  '<'  => :lt,
  '<=' => :lte,
  '>'  => :gt,
  '>=' => :gte,

  '++' => :plusplus,
  '='  => :assign,

  '|>' => :pipe_forward,
  '<|' => :pipe_backward,
  '<-' => :bind,

  '_' => :wildcard, # also used as placeholder
  '::' => :coloncolon,
  # Ternary `cond ? a : b`. Predicate-`?` attaches via the identifier
  # regex (`\??`), so this only fires when `?` stands alone.
  '?' => :question,

  '&&' => :andand,
  '||' => :oror,
}.freeze
SYMBOLS_REGEX =
Regexp.union(SYMBOLS.keys.sort_by { |k| -k.length })
INVALID_OPS =
%w[/=].freeze
INVALID_OP_REGEX =
Regexp.union(INVALID_OPS.sort_by { |k| -k.length })

Instance Method Summary collapse

Instance Method Details

#tokenize(source) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/jade/lexer.rb', line 84

def tokenize(source)
  source => Source(text:)

  scanner = StringScanner.new(text)
  tokens = []

  until scanner.eos?
    case
    when scanner.scan(/\s+/)

    when scanner.scan(INVALID_OP_REGEX)
      tokens << tok(:invalid_op, scanner)

    when scanner.scan(/\A#[^\n]*/)
      tokens << tok(:comment, scanner)

    when scanner.scan(/\A[a-z][a-z0-9_]*\??|\A_[a-z0-9_]+\??/)
      type = KEYWORDS.include?(scanner.matched) ? scanner.matched.to_sym : :identifier
      tokens << tok(type, scanner)

    when scanner.scan(SYMBOLS_REGEX)
      type = SYMBOLS.fetch(scanner.matched)
      tokens << tok(type, scanner)

    when scanner.scan(/\A(True|False)\b/)
      tokens << tok(:bool, scanner)

    when scanner.scan(/\A[A-Z][A-Za-z0-9_]*/)
      tokens << tok(:constant, scanner)

    when scanner.scan(/\d+\.\d+/)
      tokens << tok(:float, scanner)

    when scanner.scan(/\d+/)
      tokens << tok(:int, scanner)

    when scanner.scan(/\A"/)
      (tokens << tok(:quote, scanner))
        .concat(tokenize_string(scanner))

    when scanner.scan(/\A'.'/)
      tokens << Token.new(:char, scanner.matched[1], range(scanner))

    when scanner.scan(/\A'[^']*'/)
      fail "Invalid char literal #{scanner.matched.inspect}: must be a single character"

    else
      fail "FAILED TO SCAN at pos #{scanner.pos}, Next chars: #{scanner.rest[0, 20].inspect}"
    end
  end

  tokens
end