File: tree.rb

package info (click to toggle)
ruby-parslet 1.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 908 kB
  • ctags: 473
  • sloc: ruby: 5,220; makefile: 2
file content (57 lines) | stat: -rw-r--r-- 2,696 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module Parslet
  module ErrorReporter
    # An error reporter has two central methods, one for reporting errors at
    # the current parse position (#err) and one for reporting errors at a
    # given parse position (#err_at). The reporter can return an object (a
    # 'cause') that will be returned to the caller along with the information
    # that the parse failed. 
    # 
    # When reporting errors on the outer levels of your parser, these methods
    # get passed a list of error objects ('causes') from the inner levels. In
    # this default implementation, the inner levels are considered error
    # subtrees and are appended to the generated tree node at each level,
    # thereby constructing an error tree. 
    #
    # This error tree will report in parallel with the grammar structure that
    # failed. A one-to-one correspondence exists between each error in the 
    # tree and the parslet atom that produced that error. 
    #
    # The implementor is really free to use these return values as he sees
    # fit. One example would be to return an error state object from these
    # methods that is then updated as errors cascade up the parse derivation
    # tree. 
    #
    class Tree
      # Produces an error cause that combines the message at the current level
      # with the errors that happened at a level below (children).
      #
      # @param atom [Parslet::Atoms::Base] parslet that failed
      # @param source [Source] Source that we're using for this parse. (line 
      #   number information...)
      # @param message [String, Array] Error message at this level.
      # @param children [Array] A list of errors from a deeper level (or nil).
      # @return [Cause] An error tree combining children with message.
      #
      def err(atom, source, message, children=nil)
        position = source.pos
        Cause.format(source, position, message, children)
      end

      # Produces an error cause that combines the message at the current level
      # with the errors that happened at a level below (children).
      #
      # @param atom [Parslet::Atoms::Base] parslet that failed
      # @param source [Source] Source that we're using for this parse. (line 
      #   number information...)
      # @param message [String, Array] Error message at this level.
      # @param pos [Fixnum] The real position of the error.
      # @param children [Array] A list of errors from a deeper level (or nil).
      # @return [Cause] An error tree combining children with message.
      #
      def err_at(atom, source, message, pos, children=nil)
        position = pos
        Cause.format(source, position, message, children)
      end
    end
  end
end