Module: Parsec

Defined in:
lib/parsec/pos.rb,
lib/parsec/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#Column(value) ⇒ Object



10
11
12
# File 'lib/parsec/pos.rb', line 10

def Column(value)
  value.to_i
end

#incSourceColumn(source_pos, n) ⇒ Object



65
66
67
# File 'lib/parsec/pos.rb', line 65

def incSourceColumn(source_pos, n)
  SourcePos(source_pos[0], source_pos[1], source_pos[2] + n)
end

#incSourceLine(source_pos, n) ⇒ Object



61
62
63
# File 'lib/parsec/pos.rb', line 61

def incSourceLine(source_pos, n)
  SourcePos(source_pos[0], source_pos[1] + n, source_pos[2])
end

#initialPos(name) ⇒ Object



45
46
47
# File 'lib/parsec/pos.rb', line 45

def initialPos(name)
  SourcePos(name, 1, 1)
end

#Line(value) ⇒ Object



6
7
8
# File 'lib/parsec/pos.rb', line 6

def Line(value)
  value.to_i
end

#newPos(name, line, column) ⇒ Object



41
42
43
# File 'lib/parsec/pos.rb', line 41

def newPos(name, line, column)
  SourcePos(name, line, column)
end

#setSourceColumn(source_pos, n) ⇒ Object



77
78
79
# File 'lib/parsec/pos.rb', line 77

def setSourceColumn(source_pos, n)
  SourcePos(source_pos[0], source_pos[1], n)
end

#setSourceLine(source_pos, n) ⇒ Object



73
74
75
# File 'lib/parsec/pos.rb', line 73

def setSourceLine(source_pos, n)
  SourcePos(source_pos[0], n, source_pos[2])
end

#setSourceName(source_pos, n) ⇒ Object



69
70
71
# File 'lib/parsec/pos.rb', line 69

def setSourceName(source_pos, n)
  SourcePos(n, source_pos[1], source_pos[2])
end

#sourceColumn(source_pos) ⇒ Object



57
58
59
# File 'lib/parsec/pos.rb', line 57

def sourceColumn(source_pos)
  source_pos[2]
end

#sourceLine(source_pos) ⇒ Object



53
54
55
# File 'lib/parsec/pos.rb', line 53

def sourceLine(source_pos)
  source_pos[1]
end

#sourceName(source_pos) ⇒ Object



49
50
51
# File 'lib/parsec/pos.rb', line 49

def sourceName(source_pos)
  source_pos[0]
end

#SourceName(value) ⇒ Object



2
3
4
# File 'lib/parsec/pos.rb', line 2

def SourceName(value)
  value.to_s
end

#SourcePos(source_name, line = nil, column = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/parsec/pos.rb', line 14

def SourcePos(source_name, line = nil, column = nil)
  source_name, line, column = source_name, line, column
  Object.new.tap do |object|
    object.instance_variable_set "@data", [source_name, line, column]

    def object.[](index)
      @data[index]
    end

    def object.to_s
      if self[0].nil?
        show_line_column
      else
        "\"#{self[0]}\" " + show_line_column
      end
    end

    def object.inspect
      to_s
    end

    def object.show_line_column
      "(line #{self[1]}, column #{self[2]})"
    end
  end
end

#updatePosChar(source_pos, c) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/parsec/pos.rb', line 87

def updatePosChar(source_pos, c)
  case c
  when "\n"
    SourcePos(source_pos[0], source_pos[1] + 1, 1)
  when "\t"
    SourcePos(source_pos[0], source_pos[1],
      source_pos[2] + 8 - ((source_pos[2] - 1) % 8))
  else
    SourcePos(source_pos[0], source_pos[1], source_pos[2] + 1)
  end
end

#updatePosString(pos, string) ⇒ Object



81
82
83
84
85
# File 'lib/parsec/pos.rb', line 81

def updatePosString(pos, string)
  string.chars.to_a.reduce(pos) do |acc, el|
    updatePosChar acc, el
  end
end