-
Notifications
You must be signed in to change notification settings - Fork 0
Implement a loader function to load a complete SyntaxHighlighting engine from an INI file Enable compile_commands.json and improve syntax handling #88
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ba7a8a8
propagate PATH_MAX by file.h
defname 5fc6a61
create compile_commands.json (for correct using include paths in vscode)
defname dbd328e
add create and destroy functions to SyntaxHighlighting
defname 57f1e16
SyntaxHighlighting now takes the ownership of the used SyntaxDefinition
defname ded27e2
must not destroy SyntaxDefinition anymore since this is handled by Sy…
defname f42a9b9
fix bug in find_project_file()
defname 1677fa3
implement the SyntaxHighlighting_LoadFromFile() function and tests
defname d959c68
add NULL guard
defname 475d15e
Initialize error properly in SyntaxHighlighting_LoadFromFile()
defname 653e29b
guard againsst line == NULL and handle last_line == NULL in SyntaxHig…
defname ec86a65
fix bug in concat_paths()
defname 4f6f7ec
add test_definition_error to TEST_LIST
defname File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #include "loader.h" | ||
| #include <string.h> | ||
| #include <stdio.h> // snprintf | ||
| #include "io/file.h" | ||
|
|
||
| void SyntaxHighlightingLoaderError_Init(SyntaxHighlightingLoaderError *error) { | ||
| memset(error, 0, sizeof(SyntaxHighlightingLoaderError)); | ||
| error->code = SYNTAX_LOADER_NO_ERROR; | ||
| } | ||
|
|
||
|
|
||
| void SyntaxHighlightingLoaderError_Deinit(SyntaxHighlightingLoaderError *error) { | ||
| SyntaxDefinitionError_Deinit(&error->def_error); | ||
| if (error->parsing_error.message) { | ||
| free(error->parsing_error.message); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| SyntaxHighlighting *SyntaxHighlighting_LoadFromFile(const char *format, SyntaxHighlightingLoaderError *error) { | ||
| if (!error) { | ||
| return NULL; | ||
| } | ||
| SyntaxHighlightingLoaderError_Init(error); | ||
|
|
||
| if (!format) { | ||
| error->code = SYNTAX_LOADER_NO_FILENAME; | ||
| return NULL; | ||
| } | ||
|
|
||
| // 1. load the ini file | ||
| char filename[PATH_MAX]; | ||
| snprintf(filename, PATH_MAX, "data/syntax/%s.ini", format); | ||
| File *file = File_OpenProjectFile(filename, FILE_ACCESS_READ); | ||
| if (!file) { | ||
| error->code = SYNTAX_LOADER_FILE_NOT_FOUND; | ||
| return NULL; | ||
| } | ||
|
|
||
| // 2. read the ini file | ||
| char *ini = File_Read(file); | ||
| if (!ini) { | ||
| error->code = SYNTAX_LOADER_FILE_READ_ERROR; | ||
| File_Close(file); | ||
| return NULL; | ||
| } | ||
| File_Close(file); | ||
|
|
||
| // 3. parse the ini file | ||
| IniParser parser; | ||
| IniParser_Init(&parser); | ||
| IniParser_SetText(&parser, ini); | ||
| Table *table = IniParser_Parse(&parser); | ||
| if (!table) { | ||
| error->code = SYNTAX_LOADER_PARSE_ERROR; | ||
| error->parsing_error = *IniParser_GetError(&parser); | ||
| error->parsing_error.message = strdup(IniParser_GetError(&parser)->message); | ||
| IniParser_Deinit(&parser); | ||
| free(ini); | ||
| return NULL; | ||
| } | ||
| IniParser_Deinit(&parser); | ||
| free(ini); | ||
|
defname marked this conversation as resolved.
|
||
|
|
||
| // 4. create the syntax definition | ||
| SyntaxDefinition *def = SyntaxDefinition_FromTable(table, &error->def_error); | ||
| if (!def) { | ||
| Table_Destroy(table); | ||
| error->code = SYNTAX_LOADER_DEFINITION_ERROR; | ||
| return NULL; | ||
| } | ||
| Table_Destroy(table); | ||
|
|
||
| // 5. create the highlighting engine | ||
| return SyntaxHighlighting_Create(def); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #ifndef SYNTAX_LOADER_H | ||
| #define SYNTAX_LOADER_H | ||
|
|
||
| #include "highlighting.h" | ||
| #include "common/iniparser.h" | ||
|
|
||
| typedef enum { | ||
| SYNTAX_LOADER_NO_ERROR, | ||
| SYNTAX_LOADER_NO_FILENAME, | ||
| SYNTAX_LOADER_FILE_NOT_FOUND, | ||
| SYNTAX_LOADER_FILE_READ_ERROR, | ||
| SYNTAX_LOADER_PARSE_ERROR, | ||
| SYNTAX_LOADER_DEFINITION_ERROR | ||
| } SyntaxHighlightingLoaderErrorCode; | ||
|
|
||
| typedef struct { | ||
| SyntaxHighlightingLoaderErrorCode code; | ||
| SyntaxDefinitionError def_error; | ||
| ParsingError parsing_error; | ||
| } SyntaxHighlightingLoaderError; | ||
|
|
||
| void SyntaxHighlightingLoaderError_Deinit(SyntaxHighlightingLoaderError *error); | ||
|
|
||
|
|
||
| /** | ||
| * @brief Load a syntax definition and create the highlight engine. | ||
| * | ||
| * Load the syntax definition for `format` from data/sytnax/<format>.ini | ||
| * (from inside a project's folder) and create the `SyntaxHighlight` instance. | ||
| * | ||
| * @param format Format to load (something like "ini", "md"). | ||
| * @param error Will be set in the case of an error and need to be deinitialized by the caller. | ||
| * @returns | ||
| * The newly created SyntaxHighlighting Instance (ownership transfers to the caller). | ||
| * NULL there is definition file or if an error occured. | ||
| * In the case of an error `error` will be set and need to be deinitialized by the caller. | ||
| * | ||
| * Note theat `format` needs to be exactly the same as the filename. | ||
| * The convention is to use lower case. | ||
| */ | ||
| SyntaxHighlighting *SyntaxHighlighting_LoadFromFile(const char *format, SyntaxHighlightingLoaderError *error); | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.