Class: Sol::Lexer

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

Constant Summary collapse

IDENTIFIER =
/\A([a-zA-Z]\p{WORD}+\w*)/
NUMBER =
/\A([0-9]+)/
STRING =
/\A["'](.*?)["']/
OPERATOR =
/\A(\|\||&&|==|!=|<=|>=)/
WHITESPACE =
/\A([ \t\r\n]+)/
NEWLINE =
/\A([\r\n])+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLexer

Returns a new instance of Lexer.



23
24
25
26
27
# File 'lib/sol/lexer.rb', line 23

def initialize

    @KEYWORDS = ["func", "if", "true", "false", "null"]

end

Instance Attribute Details

#KEYWORDSObject (readonly)

Returns the value of attribute KEYWORDS.



9
10
11
# File 'lib/sol/lexer.rb', line 9

def KEYWORDS
  @KEYWORDS
end

Instance Method Details

#replObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sol/lexer.rb', line 52

def repl

    loop do
     
      line = Readline::readline('> ')
     
      break if line.nil? || line == 'quit'
     
      Readline::HISTORY.push(line)

      puts "#{tokenise(line)}" # Brackets are for clarity purposes
     
    end

end

#tokenise(input) ⇒ Object

This is how to implement a very simple scanner. Scan one caracter at the time until you find something to parse.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sol/lexer.rb', line 32

def tokenise(input)

    @input = input.chomp # Cleanup code by remove extra line breaks

    @i = 0 # Current character position we're parsing

    @tokens = [] # Collection of all parsed tokens in the form [:TOKEN_TYPE, value]

    while @i < @input.length

      @chunk = @input[@i..-1]

      extract_next_token
     
    end

    return @tokens
     
end