godot-gdscript-toolkit icon indicating copy to clipboard operation
godot-gdscript-toolkit copied to clipboard

Ugly exceptions during invalid code parsing

Open Scony opened this issue 2 years ago • 1 comments
trafficstars

Navigation.gd:

extends Node3D

const NAVIGATION_FIXING_TIMER_INTERVAL_S = 0.1

# air needs to be put on separate map so that air agents do not collide with terrain ones:
@onready var air = ConcreteNavigation.new(NavigationServer3D.map_create())
@onready var terrain = ConcreteNavigation.new(get_world_3d().navigation_map)

@onready var _air_region = find_child("Air").find_child("NavigationRegion3D")

var _dummy_air_agent_rid = null
var _dummy_terrain_agent_rid = null
var _dummy_agent_rids = {
	Constants.Match.Navigation.Domain.AIR: null,
	Constants.Match.Navigation.Domain.TERRAINL null,
}


class ConcreteNavigation:
	var navigation_map_rid = null

	func _init(map_rid):
		navigation_map_rid = map_rid


func _ready():
	_setup_air_navigation_map()
	_setup_navigation_fixing_timer()


func get_navigation_map_rid_by_domain(domain):
	return {
		Constants.Match.Navigation.Domain.AIR: air.navigation_map_rid,
		Constants.Match.Navigation.Domain.TERRAIN: terrain.navigation_map_rid,
	}[domain]


func _setup_air_navigation_map():
	NavigationServer3D.region_set_map(_air_region.get_region_rid(), air.navigation_map_rid)
	NavigationServer3D.map_force_update(air.navigation_map_rid)
	NavigationServer3D.map_set_active(air.navigation_map_rid, true)


func _setup_navigation_fixing_timer():
	var timer = Timer.new()
	timer.timeout.connect(_fix_navigation)
	add_child(timer)
	timer.start(NAVIGATION_FIXING_TIMER_INTERVAL_S)


func _fix_navigation():
	if _dummy_air_agent_rid == null:
		_dummy_air_agent_rid = NavigationServer3D.agent_create()
		NavigationServer3D.agent_set_position(_dummy_air_agent_rid, Vector3(-99, 0, -99))
		NavigationServer3D.agent_set_map(_dummy_air_agent_rid, air.navigation_map_rid)
	else:
		NavigationServer3D.free_rid(_dummy_air_agent_rid)
		_dummy_air_agent_rid = null

	if _dummy_terrain_agent_rid == null:
		_dummy_terrain_agent_rid = NavigationServer3D.agent_create()
		NavigationServer3D.agent_set_position(_dummy_terrain_agent_rid, Vector3(-99, 0, -99))
		NavigationServer3D.agent_set_map(_dummy_terrain_agent_rid, terrain.navigation_map_rid)
	else:
		NavigationServer3D.free_rid(_dummy_terrain_agent_rid)
		_dummy_terrain_agent_rid = null

Parsing using gdtoolkit: gdparse source/match/Navigation.gd:

Traceback (most recent call last):
  File "/home/scony/.local/lib/python3.10/site-packages/lark/lexer.py", line 590, in lex
    yield lexer.next_token(lexer_state, parser_state)
  File "/home/scony/.local/lib/python3.10/site-packages/lark/lexer.py", line 528, in next_token
    raise UnexpectedCharacters(lex_state.text, line_ctr.char_pos, line_ctr.line, line_ctr.column,
lark.exceptions.UnexpectedCharacters: <exception str() failed>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/scony/.local/lib/python3.10/site-packages/gdtoolkit/parser/__main__.py", line 67, in _parse_file_content
    tree = parser.parse(content)  # TODO: handle exceptions
  File "/home/scony/.local/lib/python3.10/site-packages/gdtoolkit/parser/parser.py", line 61, in parse
    else self._parser.parse(code)
  File "/home/scony/.local/lib/python3.10/site-packages/lark/lark.py", line 645, in parse
    return self.parser.parse(text, start=start, on_error=on_error)
  File "/home/scony/.local/lib/python3.10/site-packages/lark/parser_frontends.py", line 96, in parse
    return self.parser.parse(stream, chosen_start, **kw)
  File "/home/scony/.local/lib/python3.10/site-packages/lark/parsers/lalr_parser.py", line 41, in parse
    return self.parser.parse(lexer, start)
  File "/home/scony/.local/lib/python3.10/site-packages/lark/parsers/lalr_parser.py", line 171, in parse
    return self.parse_from_state(parser_state)
  File "/home/scony/.local/lib/python3.10/site-packages/lark/parsers/lalr_parser.py", line 193, in parse_from_state
    raise e
  File "/home/scony/.local/lib/python3.10/site-packages/lark/parsers/lalr_parser.py", line 183, in parse_from_state
    for token in state.lexer.lex(state):
  File "/home/scony/.local/lib/python3.10/site-packages/lark/indenter.py", line 45, in _process
    for token in stream:
  File "/home/scony/.local/lib/python3.10/site-packages/lark/lexer.py", line 599, in lex
    raise UnexpectedToken(token, e.allowed, state=parser_state, token_history=[last_token], terminals_by_name=self.root_lexer.terminals_by_name)
lark.exceptions.UnexpectedToken: <exception str() failed>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/scony/.local/lib/python3.10/site-packages/gdtoolkit/common/exceptions.py", line 14, in lark_unexpected_token_to_str
    return f"{exception.get_context(code)}\n{exception}".strip()
  File "/home/scony/.local/lib/python3.10/site-packages/lark/exceptions.py", line 256, in __str__
    % (self.token, self.line, self.column, self._format_expected(self.accepts or self.expected)))
  File "/home/scony/.local/lib/python3.10/site-packages/lark/exceptions.py", line 142, in _format_expected
    expected = [d[t_name].user_repr() if t_name in d else t_name for t_name in expected]
  File "/home/scony/.local/lib/python3.10/site-packages/lark/exceptions.py", line 142, in <listcomp>
    expected = [d[t_name].user_repr() if t_name in d else t_name for t_name in expected]
  File "/home/scony/.local/lib/python3.10/site-packages/lark/lexer.py", line 124, in user_repr
    return self.pattern.raw or self.name
AttributeError: 'PatternStr' object has no attribute 'raw'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/scony/.local/bin/gdparse", line 8, in <module>
    sys.exit(main())
  File "/home/scony/.local/lib/python3.10/site-packages/gdtoolkit/parser/__main__.py", line 45, in main
    success &= _parse_file(file_path, arguments)
  File "/home/scony/.local/lib/python3.10/site-packages/gdtoolkit/parser/__main__.py", line 55, in _parse_file
    return _parse_file_content(file_content, arguments, file_path)
  File "/home/scony/.local/lib/python3.10/site-packages/gdtoolkit/parser/__main__.py", line 71, in _parse_file_content
    lark_unexpected_token_to_str(exception, content),
  File "/home/scony/.local/lib/python3.10/site-packages/gdtoolkit/common/exceptions.py", line 16, in lark_unexpected_token_to_str
    return f"{exception}".strip()
  File "/home/scony/.local/lib/python3.10/site-packages/lark/exceptions.py", line 256, in __str__
    % (self.token, self.line, self.column, self._format_expected(self.accepts or self.expected)))
  File "/home/scony/.local/lib/python3.10/site-packages/lark/exceptions.py", line 142, in _format_expected
    expected = [d[t_name].user_repr() if t_name in d else t_name for t_name in expected]
  File "/home/scony/.local/lib/python3.10/site-packages/lark/exceptions.py", line 142, in <listcomp>
    expected = [d[t_name].user_repr() if t_name in d else t_name for t_name in expected]
  File "/home/scony/.local/lib/python3.10/site-packages/lark/lexer.py", line 124, in user_repr
    return self.pattern.raw or self.name
AttributeError: 'PatternStr' object has no attribute 'raw'

Scony avatar Mar 02 '23 07:03 Scony

I found this to be caused by this in my project:

			if "use_" not in activity_name:
				continue

Which should have been:

			if not "use_" in activity_name:
				continue

tavurth avatar May 10 '23 05:05 tavurth

This is fixed with latest master

Scony avatar Mar 21 '24 21:03 Scony