Module: Spectre

Included in:
StringParsing
Defined in:
lib/spectre/base/node.rb,
lib/spectre/base.rb,
lib/spectre/string.rb,
lib/spectre/generic.rb,
lib/spectre/base/parser.rb,
lib/spectre/base/closure.rb,
lib/spectre/base/grammar.rb,
lib/spectre/base/directive.rb,
lib/spectre/base/operators.rb,
lib/spectre/generic/negations.rb,
lib/spectre/string/directives.rb,
lib/spectre/string/primitives.rb,
lib/spectre/base/inputiterator.rb,
lib/spectre/generic/directives.rb,
lib/spectre/generic/primitives.rb,
lib/spectre/string/additionals.rb,
lib/spectre/string/inputiterator.rb,
lib/spectre/generic/semanticaction.rb

Overview

This is Spectre, a parser framework inspired by Boost.Spirit, which can be found at spirit.sourceforge.net/.

If you want to find out more or need a tutorial, go to spectre.rubyforge.org/ You’ll find a nice wiki there!

Author

Fabian Streitel (karottenreibe)

Copyright

Copyright © 2009 Fabian Streitel

License

Boost Software License 1.0 For further information regarding this license, you can go to www.boost.org/LICENSE_1_0.txt or read the file LICENSE distributed with this software.

Homepage

spectre.rubyforge.org/

Git repo

rubyforge.org/scm/?group_id=7618

Keeps the pre-defined semantic action classes.

Defined Under Namespace

Modules: Directives, DynVarMixin, GrammarInspectMixin, Negations, Operators, Parser, ShortcutsMixin, StringParsing Classes: Closure, ClosureAction, ClosureParser, Directive, DirectiveParser, EOIParser, Grammar, InputIterator, Match, NegatedClosureParser, Node, NothingParser, RangeParser, SequenceParser, SymParser

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(input, parser, iter_klass, pre_skip = true) ⇒ Object

Shortcut. Parses the input using parser (Parser or Node) and the InputIterator class iter_klass. pre_skip will be passed on to Node#parse.



38
39
40
# File 'lib/spectre/base.rb', line 38

def parse input, parser, iter_klass, pre_skip = true
    parser.to_p.parse iter_klass.new(input), pre_skip
end

.register_POD(klass, &block) ⇒ Object

Registers a klass as a POD with the Parser. The class will gain the method #to_p to convert it to a Parser/Node. Also it will react to the standard Operators if appropriate. The Parser for this POD is constructed by passing it to the given block, which must return the correctly instantiated Parser object.

For examples look in _spectre/std/*.rb_.



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
# File 'lib/spectre/generic.rb', line 62

def register_POD klass, &block
    klass.class_eval do
        def to_p
            Spectre::Parser.from_POD(self).to_p
        end

        old = {}
        single_methods = [:+, :*]
        dual_methods = [:>>, :**, :|, :&, :%, :^, :-]

        (single_methods + dual_methods).each do |sym|
            old[sym] = self.instance_method sym if self.instance_methods.include? sym.to_s
        end

        single_methods.each do |sym|
            define_method sym do |*args|
                if args.empty?
                    self.to_p.send sym
                else
                    if old[sym] then old[sym].bind(self).call *args
                    else raise NoMethodError.new "undefined method '#{sym.to_s}' for #{self.inspect}:#{self.class}"
                    end
                end
            end
        end

        dual_methods.each do |sym|
            define_method sym do |*args|
                if args[0].is_a? Spectre::Node or args[0].is_a? Spectre::Parser
                    self.to_p.send sym, args[0].to_p
                else
                    if old[sym] then old[sym].bind(self).call *args
                    else raise NoMethodError.new "undefined method '#{sym.to_s}' for #{self.inspect}:#{self.class}"
                    end
                end
            end
        end
    end

    Parser.PODs[klass] = block
end

Instance Method Details

#closedObject

The ClosureParser shortcut is closed.



94
# File 'lib/spectre/base/closure.rb', line 94

ShortcutsMixin.register_shortcut :closed => ClosureParser

#symObject

The SymParser shortcut is sym.



266
# File 'lib/spectre/base/grammar.rb', line 266

ShortcutsMixin.register_shortcut :sym => SymParser