lib/term_utils/ap/parser.rb in term_utils-0.4.0 vs lib/term_utils/ap/parser.rb in term_utils-0.5.0

- old
+ new

@@ -1,8 +1,8 @@ -# frozen-string-literal: true +# frozen_string_literal: true -# Copyright (C) 2020 Thomas Baron +# Copyright (C) 2023 Thomas Baron # # This file is part of term_utils. # # term_utils is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -25,11 +25,12 @@ end # Parses a given list of arguments. # @param syntax [Syntax] # @param arguments [Array<String>] - # @param opts [Hash] + # @param opts [Hash<Symbol, Object>] + # @option opts [Boolean] :strict Whether the Syntax must be considered as strict. # @return [Result] # @raise [ParseError] # @raise [SyntaxError] def parse_arguments(syntax, arguments, opts = {}, &block) syntax = syntax.dup @@ -50,11 +51,11 @@ # @return [Array<String>, nil] Replacements on success, nil otherwise. def self.match_shortcut_flag(shortcut_flags, arg) shortcut_flags.each do |label, flag| next unless arg.start_with? label - return [flag.label, arg[label.length..-1]] + return [flag.label, arg[label.length..]] end nil end @@ -94,19 +95,19 @@ end end unless flagged_params.key? arguments.first # Unknown flag # End of parsing - raise TermUtils::AP::ParseError, 'flagged parameter unexpected' if opts.fetch(:strict, false) + raise TermUtils::AP::ParseError.new(message: 'flagged parameter unexpected', fault: arguments.first) if opts.fetch(:strict, false) break end param = flagged_params[arguments.first] if param.occur_bounded? && (fp_occ[param.id] >= param.max_occurs) # Max occurs reached - raise TermUtils::AP::ParseError, "occur limit reached (#{param.id})" if opts.fetch(:strict, false) + raise TermUtils::AP::ParseError.new(message: 'occur limit reached', parameter: param.id, fault: arguments.first) if opts.fetch(:strict, false) break end fp_occ[param.id] += 1 @@ -115,19 +116,19 @@ parse0_param(param_res, param, arguments) else # Unflagged if unflagged_params.empty? # End of parsing - raise TermUtils::AP::ParseError, 'unflagged parameter unexpected' if opts.fetch(:strict, false) + raise TermUtils::AP::ParseError.new(message: 'unflagged parameter unexpected', fault: arguments.first) if opts.fetch(:strict, false) break end param = unflagged_params.first if arguments.first == '--' # End of parameter - raise TermUtils::AP::ParseError, "parameter not consumed (#{param.id})" if up_occ < param.min_occurs + raise TermUtils::AP::ParseError.new(message: 'parameter not consumed', parameter: param.id) if up_occ < param.min_occurs arguments.shift unflagged_params.shift up_occ = 0 next @@ -135,11 +136,11 @@ up_occ += 1 param_res = TermUtils::AP::ParameterResult.new(result, param) case parse0_param(param_res, param, arguments) when :esc_param - raise TermUtils::AP::ParseError, "parameter not consumed (#{param.id})" if up_occ < param.min_occurs + raise TermUtils::AP::ParseError.new(message: 'parameter not consumed', parameter: param.id) if up_occ < param.min_occurs unflagged_params.shift up_occ = 0 else if !param.multiple_occurs? || (param.occur_bounded? && (up_occ >= param.max_occurs)) @@ -151,11 +152,11 @@ end # loop # Check min occurs syntax.parameters.each do |p| next if result.find_parameters(p.id).length >= p.min_occurs - raise TermUtils::AP::ParseError, "parameter not consumed (#{p.id})" + raise TermUtils::AP::ParseError.new(message: 'parameter not consumed', parameter: p.id) end end # Parses with a given Parameter. # @param result [ParameterResult] @@ -168,27 +169,27 @@ loop do break if arts.empty? if arguments.empty? # End of arguments - raise TermUtils::AP::ParseError, 'article not consumed' if occ < arts.first.min_occurs - raise TermUtils::AP::ParseError, 'article not consumed' if self.class.eval_article_min_occurs(arts[1..-1]) > 0 + raise TermUtils::AP::ParseError.new(message: 'article not consumed', parameter: param.id) if occ < arts.first.min_occurs + raise TermUtils::AP::ParseError.new(message: 'article not consumed', parameter: param.id) if self.class.eval_article_min_occurs(arts[1..]) > 0 break end if arguments.first == '-' # End of article - raise TermUtils::AP::ParseError, 'article not consumed' if occ < arts.first.min_occurs + raise TermUtils::AP::ParseError.new(message: 'article not consumed', parameter: param.id) if occ < arts.first.min_occurs arguments.shift arts.shift occ = 0 next elsif arguments.first.start_with? '-' # End of parameter - raise TermUtils::AP::ParseError, 'article not consumed' if occ < arts.first.min_occurs - raise TermUtils::AP::ParseError, 'article not consumed' if self.class.eval_article_min_occurs(arts[1..-1]) > 0 + raise TermUtils::AP::ParseError.new(message: 'article not consumed', parameter: param.id) if occ < arts.first.min_occurs + raise TermUtils::AP::ParseError.new(message: 'article not consumed', parameter: param.id) if self.class.eval_article_min_occurs(arts[1..]) > 0 if arguments.first == '--' arguments.shift return :esc_param end