Skip to content

adjust parsing to raise nicer exceptions #213

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

snoyer
Copy link

@snoyer snoyer commented Aug 30, 2023

The current parsing works great on well formed path but could be improved when it comes to handling invalid cases:

  1. the "Unallowed implicit command" error message mentions an internal token index
  2. invalid tokens are silently dropped
  3. the parser can run out of tokens and let an IndexError: pop from empty list through
Path('1,2')
ValueError: Unallowed implicit command in 1,2, position -1

Path('M 100 100 L 200 200 Z 100 200')
ValueError: Unallowed implicit command in M 100 100 L 200 200 Z 100 200, position 7

Path('M 0 1 L 0 0 L 0 0z1')
ValueError: Unallowed implicit command in M 0 1 L 0 0 L 0 0z1, position 8

Path('M 1 2 3')
IndexError: pop from empty list

Path('M 0 1 N 2 3')
Path(Line(start=1j, end=(2+3j)))

Path('M 0 1 C 1 2 3 4\n      5 foo')
IndexError: pop from empty list

This PR catches cases 2. and 3. and raises a standard SyntaxError highlighting the error location in the d string in all 3 cases:

Path('1,2')
  File "<svg-d-string>", line 1
    1,2
    ^
SyntaxError: missing command

Path('M 100 100 L 200 200 Z 100 200')
  File "<svg-d-string>", line 1
    M 100 100 L 200 200 Z 100 200
                          ^^^
SyntaxError: missing command

Path('M 0 1 L 0 0 L 0 0z1')
  File "<svg-d-string>", line 1
    M 0 1 L 0 0 L 0 0z1
                      ^
SyntaxError: missing command

Path('M 1 2 3')
  File "<svg-d-string>", line 1
    M 1 2 3
          ^
SyntaxError: not enough arguments

Path('M 0 1 N 2 3')
  File "<svg-d-string>", line 1
    M 0 1 N 2 3
         ^^^
SyntaxError: invalid token ' N '

Path('M 0 1 C 1 2 3 4\n      5 foo')
  File "<svg-d-string>", line 2
    5 foo
     ^^^^
SyntaxError: invalid token ' foo'

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant