Class: Bijou::Parse::LexerInput

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

Overview

Contains operations and state that is shared between the different lexers. This simplifies the process of tracking the line and column numbers of the input files. Also maintains a lookahead list of characters so the lexers don’t have to. This list is only used for named tags, which are reserved words, so its length may be constrained.

Constant Summary collapse

MaxLookahead =

Must be > than max name in named tags (<%name>)

15

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, diagnostics) ⇒ LexerInput

Returns a new instance of LexerInput.



31
32
33
34
35
36
37
38
39
# File 'lib/bijou/lexer.rb', line 31

def initialize(file, diagnostics)
  @diagnostics = diagnostics
  @file = file
  @character = 0
  @line = 1
  @column = 1
  @lookahead = []
  @columns = []
end

Instance Attribute Details

#diagnosticsObject (readonly)

Returns the value of attribute diagnostics.



45
46
47
# File 'lib/bijou/lexer.rb', line 45

def diagnostics
  @diagnostics
end

#fileObject (readonly)

Returns the value of attribute file.



45
46
47
# File 'lib/bijou/lexer.rb', line 45

def file
  @file
end

Instance Method Details

#characterObject



50
51
52
# File 'lib/bijou/lexer.rb', line 50

def character
  @character
end

#character=(n) ⇒ Object



47
48
49
# File 'lib/bijou/lexer.rb', line 47

def character=(n)
  @character = n
end

#closeObject



41
42
43
# File 'lib/bijou/lexer.rb', line 41

def close
  @file.close
end

#columnObject



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

def column
  @column
end

#getcObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bijou/lexer.rb', line 65

def getc()
  @character += 1
  ch = @file.getc
#puts "getc #{@character}: #{ch.chr}"
  if ch == 10
    @line += 1
    @columns.push(@column)
    if @columns.length > MaxLookahead
      @columns.shift
    end
    @column = 1
  elsif ch == 9
    @column += 2 # REVIEW: We assume tabs of two spaces
  else
    @column += 1
  end
  @lookahead.push(ch)
  if @lookahead.length > MaxLookahead 
    @lookahead.shift 
  end
  return ch
end

#lineObject



57
58
59
# File 'lib/bijou/lexer.rb', line 57

def line
  @line
end

#line=(n) ⇒ Object



54
55
56
# File 'lib/bijou/lexer.rb', line 54

def line=(n)
  @line = n
end

#popObject

Pop a character from the lookahead list back to the file stream.



103
104
105
106
107
108
109
# File 'lib/bijou/lexer.rb', line 103

def pop
  if @lookahead.length > 0
    ungetc(@lookahead.pop)
  else
    raise "lookahead underflow"
  end
end

#ungetc(ch) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bijou/lexer.rb', line 88

def ungetc(ch)
#puts "ungetc #{@character}: #{ch.chr}"
  if ch == 10
    @line -= 1
    if @columns.length > 0
      @column = @columns.pop
    else
      raise "column lookahead underflow"
    end
  end
  @character -= 1
  @file.ungetc(ch)
end