Extract `check_word` function

pull/504/merge
ObserverOfTime 2023-08-10 00:14:35 +07:00
parent 148ad3ddba
commit ee045638fc
No known key found for this signature in database
GPG Key ID: 8A2DEA1DBAEBCA9E
5 changed files with 35 additions and 57 deletions

1
.gitattributes vendored

@ -2,3 +2,4 @@
**/src/*.json linguist-generated **/src/*.json linguist-generated
**/src/parser.c linguist-generated **/src/parser.c linguist-generated
**/src/tree_sitter/parser.h linguist-generated

@ -15,9 +15,20 @@ enum TokenType {
/// Advance the lexer to the next token /// Advance the lexer to the next token
static inline void advance(TSLexer *lexer) { lexer->advance(lexer, false); } static inline void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
/// Check if the character is valid in PITarget
/// @private
static inline bool is_valid_pi_char(int32_t chr) { static inline bool is_valid_pi_char(int32_t chr) {
return isalnum(chr) || chr == '_' || chr == ':' || chr == '.' || return isalnum(chr) || chr == '_' || chr == ':' || chr == '.' || chr == '-' || chr == L'·';
chr == '-' || chr == L'·'; }
/// Check if the lexer matches the given word
/// @private
static inline bool check_word(TSLexer *lexer, const char *const word) {
for (int j = 0; word[j] != '\0'; ++j) {
if (word[j] != lexer->lookahead) return false;
advance(lexer);
}
return true;
} }
/// Scan for the target of a PI node /// Scan for the target of a PI node
@ -45,35 +56,8 @@ static bool scan_pi_target(TSLexer *lexer, const bool *valid_symbols) {
bool last_char_hyphen = lexer->lookahead == '-'; bool last_char_hyphen = lexer->lookahead == '-';
advance(lexer); advance(lexer);
if (last_char_hyphen) { if (last_char_hyphen) {
// scan for stylesheet/model and disallow that if (valid_symbols[XmlModel] && check_word(lexer, "model")) return false;
if (valid_symbols[XmlModel]) { if (valid_symbols[XmlStylesheet] && check_word(lexer, "stylesheet")) return false;
const char *const word = "model";
int j = 0;
while (word[j] != '\0') {
if (word[j] != lexer->lookahead) {
break;
}
j++;
advance(lexer);
}
if (word[j] == '\0') {
return false;
}
}
if (valid_symbols[XmlStylesheet]) {
const char *const word = "stylesheet";
int j = 0;
while (word[j] != '\0') {
if (word[j] != lexer->lookahead) {
break;
}
j++;
advance(lexer);
}
if (word[j] == '\0') {
return false;
}
}
} }
} else { } else {
return false; return false;
@ -97,9 +81,7 @@ static bool scan_pi_target(TSLexer *lexer, const bool *valid_symbols) {
static bool scan_pi_content(TSLexer *lexer) { static bool scan_pi_content(TSLexer *lexer) {
bool advanced_once = false; bool advanced_once = false;
while (!lexer->eof(lexer) && while (!lexer->eof(lexer) && lexer->lookahead != '\n' && lexer->lookahead != '?') {
lexer->lookahead != '\n' &&
lexer->lookahead != '?') {
advanced_once = true; advanced_once = true;
advance(lexer); advance(lexer);
} }
@ -159,7 +141,6 @@ static bool scan_comment(TSLexer *lexer) {
} }
/// Define the boilerplate functions of the scanner /// Define the boilerplate functions of the scanner
/// @param name the name of the language
#define SCANNER_BOILERPLATE(name) \ #define SCANNER_BOILERPLATE(name) \
void *tree_sitter_##name##_external_scanner_create() { return NULL; } \ void *tree_sitter_##name##_external_scanner_create() { return NULL; } \
\ \

@ -13,8 +13,9 @@ extern "C" {
#define ts_builtin_sym_end 0 #define ts_builtin_sym_end 0
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024 #define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
#ifndef TREE_SITTER_API_H_
typedef uint16_t TSStateId; typedef uint16_t TSStateId;
#ifndef TREE_SITTER_API_H_
typedef uint16_t TSSymbol; typedef uint16_t TSSymbol;
typedef uint16_t TSFieldId; typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage; typedef struct TSLanguage TSLanguage;
@ -129,16 +130,9 @@ struct TSLanguage {
* Lexer Macros * Lexer Macros
*/ */
#ifdef _MSC_VER
#define UNUSED __pragma(warning(suppress : 4101))
#else
#define UNUSED __attribute__((unused))
#endif
#define START_LEXER() \ #define START_LEXER() \
bool result = false; \ bool result = false; \
bool skip = false; \ bool skip = false; \
UNUSED \
bool eof = false; \ bool eof = false; \
int32_t lookahead; \ int32_t lookahead; \
goto start; \ goto start; \
@ -172,7 +166,7 @@ struct TSLanguage {
* Parse Table Macros * Parse Table Macros
*/ */
#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT) #define SMALL_STATE(id) id - LARGE_STATE_COUNT
#define STATE(id) id #define STATE(id) id
@ -182,7 +176,7 @@ struct TSLanguage {
{{ \ {{ \
.shift = { \ .shift = { \
.type = TSParseActionTypeShift, \ .type = TSParseActionTypeShift, \
.state = (state_value) \ .state = state_value \
} \ } \
}} }}
@ -190,7 +184,7 @@ struct TSLanguage {
{{ \ {{ \
.shift = { \ .shift = { \
.type = TSParseActionTypeShift, \ .type = TSParseActionTypeShift, \
.state = (state_value), \ .state = state_value, \
.repetition = true \ .repetition = true \
} \ } \
}} }}

@ -2623,6 +2623,14 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "CharData" "name": "CharData"
},
{
"type": "STRING",
"value": "xml-model"
},
{
"type": "STRING",
"value": "xml-stylesheet"
} }
], ],
"inline": [ "inline": [

@ -13,8 +13,9 @@ extern "C" {
#define ts_builtin_sym_end 0 #define ts_builtin_sym_end 0
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024 #define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
#ifndef TREE_SITTER_API_H_
typedef uint16_t TSStateId; typedef uint16_t TSStateId;
#ifndef TREE_SITTER_API_H_
typedef uint16_t TSSymbol; typedef uint16_t TSSymbol;
typedef uint16_t TSFieldId; typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage; typedef struct TSLanguage TSLanguage;
@ -129,16 +130,9 @@ struct TSLanguage {
* Lexer Macros * Lexer Macros
*/ */
#ifdef _MSC_VER
#define UNUSED __pragma(warning(suppress : 4101))
#else
#define UNUSED __attribute__((unused))
#endif
#define START_LEXER() \ #define START_LEXER() \
bool result = false; \ bool result = false; \
bool skip = false; \ bool skip = false; \
UNUSED \
bool eof = false; \ bool eof = false; \
int32_t lookahead; \ int32_t lookahead; \
goto start; \ goto start; \
@ -172,7 +166,7 @@ struct TSLanguage {
* Parse Table Macros * Parse Table Macros
*/ */
#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT) #define SMALL_STATE(id) id - LARGE_STATE_COUNT
#define STATE(id) id #define STATE(id) id
@ -182,7 +176,7 @@ struct TSLanguage {
{{ \ {{ \
.shift = { \ .shift = { \
.type = TSParseActionTypeShift, \ .type = TSParseActionTypeShift, \
.state = (state_value) \ .state = state_value \
} \ } \
}} }}
@ -190,7 +184,7 @@ struct TSLanguage {
{{ \ {{ \
.shift = { \ .shift = { \
.type = TSParseActionTypeShift, \ .type = TSParseActionTypeShift, \
.state = (state_value), \ .state = state_value, \
.repetition = true \ .repetition = true \
} \ } \
}} }}