Class: Ovec::Parser

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

Overview

TODO: verbatimy; TeX prikazy obsahujici kusy k ovlnkovani

Constant Summary collapse

NORMAL_REGEX =
/\A([^\\$%])+/
COMMAND_REGEX =

A command: (letters) or (a single nonletter)

/\A\\([a-z@A-Z0-9]+|[^a-zA-Z])/
COMMENT_REGEX =
/\A%.*$/
TEXT_COMMANDS =
%w(textit textbf textsc title author)
NONTEXT_CONTENT_COMMANDS =

TODO: seznam prikazu, ve kterych se nevlnkuje

%w()
VERBATIM_COMMANDS =

TODO: treba begin, end, …

%w(verbatim verb)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug: false) ⇒ Parser

Returns a new instance of Parser.



43
44
45
# File 'lib/ovec/parser.rb', line 43

def initialize(debug: false)
  @debug = debug
end

Class Method Details

.delimiter_right_side(left) ⇒ Object



47
48
49
50
51
# File 'lib/ovec/parser.rb', line 47

def self.delimiter_right_side(left)
  {
    '{' => '}', '[' => ']', '(' => ')', '<' => '>'
  }[left] || left
end

Instance Method Details

#eat_node(code, index) ⇒ Object

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/ovec/parser.rb', line 53

def eat_node(code, index)
  to_eat = code[index...code.length]

  if to_eat.empty?
    raise ParseError, "Parsing an empty string"
  end

  # Cut off comments
  match = COMMENT_REGEX.match(to_eat)
  unless match.nil?
    match = match[0]
    node = CommentNode.new(match)

    debug "Eaten #{match.length} chars of comments."
    return node, match.length
  end

  # Cut off normal text
  match = NORMAL_REGEX.match(to_eat)
  unless match.nil?
    match = match[0]
    node = TextNode.new(match)

    debug "Eaten #{match.length} chars of normal text."
    return node, match.length
  end

  # Parse a command. If the command looks like a text-command, try to eat everything between { and }.
  match = COMMAND_REGEX.match(to_eat)
  unless match.nil?
    command = match[1]
    match_len = match[0].length
    debug "Eating command #{command}."

    if TEXT_COMMANDS.include?(command) || NONTEXT_CONTENT_COMMANDS.include?(command)
      if to_eat.length > match_len && to_eat[match_len] == '{'
        debug "Looks like a text command, trying to parse recursively."
        begin
          other_side = find_other_side(to_eat, match_len, '{', '}')

          content = parse(code[index + match_len + 1...index + other_side])
          # TODO: rename textcommands to something better: command with params?
          node = TextCommandsNode.new(command, content, text: TEXT_COMMANDS.include?(command))

          debug "Eaten #{other_side + 1} chars of text command."
          return node, (other_side + 1)
        rescue ParseError
          debug "Parse error encountered. Giving up on this node."
        end
      end
    end

    if VERBATIM_COMMANDS.include?(command)
      if to_eat.length <= match_len
        raise ParseError, "Verbatim at EOF with no delimiter."
      end

      left_delimiter = to_eat[match_len]
      right_delimiter = self.class.delimiter_right_side(left_delimiter)

      # TODO: nesting in verbatims ???
      other_side = find_other_side(to_eat, match_len, left_delimiter, right_delimiter)

      content = to_eat[match_len + 1...other_side]

      node = VerbatimNode.new(command, left_delimiter, content)

      debug "Eaten #{other_side + 1} chars of verbatim."
      return node, (other_side + 1)
    end

    node = CommandsNode.new(command)
    
    debug "Eaten #{match_len} chars of commands."
    return node, match_len
  end

  if to_eat.length > 1 && to_eat[0..1] == "$$"
    # Lookaround assertion to handle escaped dollars in math.
    ending = to_eat[2...to_eat.length].index(/(?<!\\)\$\$/)
    
    if ending.nil?
      raise ParseError, "Unterminated $$ starting at #{index}"
    end

    node = MathNode.new(MathNode::DISPLAY, to_eat[2...ending+2])
    eaten = ending + 4
    debug "Eaten #{eaten} chars of inline math."
    return node, eaten
  end

  if to_eat[0] == '$'
    # Lookaround assertion to handle escaped dollars in math.
    ending = to_eat[1...to_eat.length].index(/(?<!\\)\$/)

    if ending.nil?
      raise ParseError, "Unterminated $ starting at #{index}"
    end

    node = MathNode.new(MathNode::INLINE, to_eat[1...ending+1])
    eaten = ending + 2
    debug "Eaten #{eaten} chars of inline math."
    return node, eaten
  end

  raise ParseError, "Don't know how to parse '#{to_eat}'."
end

#parse(code) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ovec/parser.rb', line 161

def parse(code)
  result = CombinedNode.new

  index = 0
  while index < code.length
    node, shift = eat_node(code, index)
    index += shift

    result << node
  end

  return result
end