module SrlRuby
Constants
- Grammar
And now build the grammar and make it accessible via a global constant
- Rley::Syntax::Grammar
- VERSION
Public Class Methods
load_file(filename)
click to toggle source
Compile the SRL expression in given filename into a Regexp object. @param filename [String] Name of SRL file to parse @return [Regexp] A regexp object equivalent to the SRL expression.
# File lib/srl_ruby.rb, line 12 def self.load_file(filename) source = nil File.open(filename, 'r') { |f| source = f.read } return source if source.nil? || source.empty? return parse(source) end
parse(source)
click to toggle source
Compile the given SRL expression into its Regexp equivalent. @param source [String] SRL expression to parse @return [Regexp] A regexp object equivalent to the SRL expression. @example Matching a (signed) integer literal
some_srl =<<-SRL begin with (one of "+-") optional, digit once or more, must end SRL regex = SrlRuby::API.parse(some_srl) # => regex == /^[+\-]?\d+$/
# File lib/srl_ruby.rb, line 31 def self.parse(source) # Create a Rley facade object engine = Rley::Engine.new { |cfg| cfg.diagnose = true } # Step 1. Load SRL grammar engine.use_grammar(SrlRuby::Grammar) lexer = SrlRuby::Tokenizer.new(source) result = engine.parse(lexer.tokens) unless result.success? # Stop if the parse failed... line1 = "Parsing failed\n" line2 = "Reason: #{result.failure_reason.message}" raise StandardError, line1 + line2 end # Generate an abstract syntax tree (AST) from the parse result engine.configuration.repr_builder = SrlRuby::ASTBuilder ast_ptree = engine.convert(result) # Now output the regexp literal root = ast_ptree.root options = root.is_a?(Regex::MatchOption) ? root.combine_opts : nil return Regexp.new(root.to_str, options) end