Class: Astrapi::Lexer

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

Constant Summary collapse

IDENT =

.….….….….….….….….….….….…

/\AIDENT/
INT =
/\AINT/
FLOAT =
/\AFLOAT/
STRING =
/\ASTRING/
MODULE =

.….….….….….….….….….….….…

/\Amodule$/
CLASS =
/\Aclass$/
END_ =
/\Aend$/
ATTR =
/\Aattr$/
NEWLINE =

.….….….….….….….….….….….…

/[\n]/
SPACE =
/[ \t]+/
LPAREN =

.….….….punctuation.….….….….….…

/\A\(/
RPAREN =
/\A\)/
COMMENT =
/\A\#(.*)/
ARROW =

.….….….operators.….….….….….…..

/\A\=\>/
LBRACK =
/\A\[/
RBRACK =
/\A\]/
LT =
/\A</
IDENTIFIER =

.….….….literals.….….….….….….

/\A[a-zA-Z]+[a-zA-Z_0-9]*/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str = '') ⇒ Lexer

Returns a new instance of Lexer.



61
62
63
64
65
# File 'lib/lexer.rb', line 61

def initialize str=''
  init(str)
  @suppress_space=true
  @suppress_comment=false
end

Instance Attribute Details

#suppress_commentObject

Returns the value of attribute suppress_comment.



59
60
61
# File 'lib/lexer.rb', line 59

def suppress_comment
  @suppress_comment
end

Instance Method Details

#init(str) ⇒ Object



67
68
69
70
# File 'lib/lexer.rb', line 67

def init str
  @ss=StringScanner.new(str)
  @line=0
end

#next_tokenObject

next token can detect spaces length



82
83
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
137
# File 'lib/lexer.rb', line 82

def next_token

  if @ss.bol?
    @line+=1
    @old_pos=@ss.pos
  end

  position=[@line,@ss.pos-@old_pos+1]

  return :eos if @ss.eos?

  case
  when text = @ss.scan(NEWLINE)
    next_token()
  when text = @ss.scan(SPACE)
    next_token()
  when text = @ss.scan(COMMENT)
    next_token()
  when text = @ss.scan(ARROW)
    return Token.new [:arrow,text,position]
  when text = @ss.scan(LT)
    return Token.new [:lt,text,position]
  when text = @ss.scan(LBRACK)
    return Token.new [:lbrack,text,position]
  when text = @ss.scan(RBRACK)
    return Token.new [:rbrack,text,position]
  when text = @ss.scan(IDENTIFIER)
    case
    when value = text.match(IDENT)
      return Token.new [:IDENT,text,position]
    when value = text.match(FLOAT)
      return Token.new [:FLOAT,text,position]
    when value = text.match(INT)
      return Token.new [:INT,text,position]
    when value = text.match(STRING)
      return Token.new [:STRING,text,position]
    when value = text.match(MODULE)
      return Token.new [:module,text,position]
    when value = text.match(CLASS)
      return Token.new [:class,text,position]
    when value = text.match(END_)
      return Token.new [:end,text,position]
    when value = text.match(ATTR)
      return Token.new [:attr,text,position]
    when value = text.match(LPAREN)
      return Token.new [:lparen,text,position]
    when value = text.match(RPAREN)
      return Token.new [:rparen,text,position]
    else
      return Token.new [:identifier,text,position]
    end
  else
    x = @ss.getch
    return Token.new [x, x,position]
  end
end

#tokenize(str) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/lexer.rb', line 72

def tokenize str
  @tokens=[]
  init(str)
  until @ss.eos?
    @tokens << next_token()
  end
  return @tokens[0..-2]
end