Skip to content

Commit

Permalink
Merge pull request #3233 from hlindberg/PUP-3514_future_parser_errors…
Browse files Browse the repository at this point in the history
…_from_parser

(PUP-3514) Fix broken errors from future parser
  • Loading branch information
zaphod42 committed Oct 29, 2014
2 parents fbc7e16 + 5b46b95 commit 3966e89
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
29 changes: 20 additions & 9 deletions lib/puppet/pops/parser/parser_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,25 @@ def classname(name)
[namespace, name].join('::').sub(/^::/, '')
end

# Raises a Parse error.
def error(value, message, options = {})
# Raises a Parse error with location information. Information about file is always obtained from the
# lexer. Line and position is produced if the given semantic is a Positioned object and have been given an offset.
#
def error(semantic, message)
semantic = semantic.current() if semantic.is_a?(Puppet::Pops::Model::Factory)
# Adapt the model so it is possible to get location information.
# The model may not have been added to the source tree, so give it the lexer's locator
# directly instead of searching for the root Program where the locator is normally stored.
#
if semantic.is_a?(Puppet::Pops::Model::Positioned)
adapter = Puppet::Pops::Adapters::SourcePosAdapter.adapt(semantic)
adapter.locator = @lexer.locator
else
adapter = nil
end
except = Puppet::ParseError.new(message)
except.line = options[:line] || value[:line]
except.file = options[:file] || value[:file]
except.pos = options[:pos] || value[:pos]

except.file = @lexer.locator.file
except.line = adapter.line if adapter
except.pos = adapter.pos if adapter
raise except
end

Expand Down Expand Up @@ -114,10 +126,9 @@ def on_error(token,value,stack)
end

# Parses a String of pp DSL code.
# @todo make it possible to pass a given origin
#
def parse_string(code)
@lexer.string = code
def parse_string(code, path = '')
@lexer.lex_string(code, path)
_parse()
end

Expand Down
14 changes: 14 additions & 0 deletions spec/unit/pops/parser/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,18 @@
expect(adapter.offset).to eq(10)
expect(adapter.length).to eq(0)
end

it "should raise an error with position information when error is raised from within parser" do
parser = Puppet::Pops::Parser::Parser.new()
the_error = nil
begin
parser.parse_string("File [1] { }", 'fakefile.pp')
rescue Puppet::ParseError => e
the_error = e
end
expect(the_error).to be_a(Puppet::ParseError)
expect(the_error.file).to eq('fakefile.pp')
expect(the_error.line).to eq(1)
expect(the_error.pos).to eq(6)
end
end

0 comments on commit 3966e89

Please sign in to comment.