WerWolv / ImHex

🔍 A Hex Editor for Reverse Engineers, Programmers and people who value their retinas when working at 3 AM.
https://imhex.werwolv.net
GNU General Public License v2.0
40.06k stars 1.76k forks source link

[Bug] `import` fail to parse dot syntax statement if it encounter a keyword #1639

Open Naheulf opened 2 months ago

Naheulf commented 2 months ago

Operating System

Windows 10 x64

What's the issue you encountered?

Doing few refactor : tried to move a "match" related function to a dedicated file. (Note: it's a Y part of a XY Problem, at first I tried to find a sugar way to be warned if an area with an unknown meaning have a new unseen value.

After moving the content to a new file and tried to import it I got that error.

E: error: Failed to resolve import: Could not find file tools E: --> in D:\Users\Perso\Scripts\ImHex\includes\resident.pat:13:8 E: >> from :13:8 E: 13 | import tools.match; E: ^^^^^ E: error: Expected ';' at end of statement, got Separator (.). E: --> in D:\Users\Perso\Scripts\ImHex\includes\resident.pat:13:8 E: >> from :13:8 E: 13 | import tools.match; E: ^^^^^ I: Evaluation took 0.129612s

How can the issue be reproduced?

Create a project. Create the includes directory along the project file. In that folder create a tools folder. In the tool folder create a match.pat file (the file content does not matter, you can keep it empty) In ImHex open any file or create an new one In the pattern write import tools.match; Click on the play button to evaluate the pattern.

ImHex Version

1.33.2

ImHex Build Type

Installation type

MSI

Additional context?

paxcut commented 2 months ago

This is not a bug. When the import statement is tokenized each identifier separated by a dot becomes a token. Any token named 'match' must be a keyword token so tools.match cannot be resolved into the file match.pat located in the tools folder.

Note that in most languages (including c and c++) keywords cannot be used as identifiers either and pattern language is no exception. You can't use built-in type names as identifiers either. Valid identifiers must not be any of the keywords or built-in type names or named operators defined in the language and must start with a letter or '_' and can only contain alphanumeric chars and the '_' char.

Also note that

import "tools/match.pat";

is perfectly fine because the file name is a string so it is not broken into identifier tokens as the entire becomes a string token and string tokens can contain any char sequence.