Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add byte offsets for Tokens #186

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

Conversation

ksurent
Copy link

@ksurent ksurent commented Apr 14, 2016

This patch adds a new method that returns positions of the first and the last bytes of a Token (byte span).
It comes in handy e.g. when building smart code viewer apps (PPI::Element->location() is not precise enough) or working with code analysis libraries (all libraries I'm aware of require byte offsets).

byte_span() allows to refer to specific ranges of source code within a document

note that this works on Token level to allow referring to a specific token
within an Element (e.g. PPI::Token::Word within PPI::Statement::Include)

for every token we now store its starting byte position and ending
byte is deduced as start byte + token byte length

this works for all token types except HereDoc, which requires some
extra bookkeeping
see comments in lib/PPI/Token/HereDoc.pm for further details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant