Add if and its test cases

This commit is contained in:
Uy Ha 2021-06-17 22:32:18 +02:00
parent d843d2677f
commit eb032187a2
5 changed files with 8478 additions and 2472 deletions

85
corpus/condition.txt Normal file
View file

@ -0,0 +1,85 @@
====================
Empty if [condition]
====================
if(cond)
endif()
---
(source_file
(if_condition
(if_command
(if)
(argument (unquoted_argument))
)
(endif_command (endif))
)
)
===========================
Empty if elseif [condition]
===========================
if(cond)
elseif(cond)
endif()
---
(source_file
(if_condition
(if_command
(if)
(argument (unquoted_argument))
)
(elseif_command
(elseif)
(argument (unquoted_argument))
)
(endif_command (endif))
)
)
================================
Empty if elseif else [condition]
================================
if(cond)
elseif(cond)
else()
endif()
---
(source_file
(if_condition
(if_command
(if)
(argument (unquoted_argument))
)
(elseif_command
(elseif)
(argument (unquoted_argument))
)
(else_command (else))
(endif_command (endif))
)
)
==========================================
If with one command invocation [condition]
==========================================
if(cond)
message(STATUS)
endif()
---
(source_file
(if_condition
(if_command
(if)
(argument (unquoted_argument))
)
(normal_command (identifier) (argument (unquoted_argument)))
(endif_command (endif))
)
)

View file

@ -1,3 +1,50 @@
if_args = [
"1",
"ON",
"YES",
"TRUE",
"Y",
"0",
"OFF",
"NO",
"FALSE",
"N",
"IGNORE",
"NOTFOUND",
"NOT",
"AND",
"OR",
"COMMAND",
"POLICY",
"TARGET",
"TEST",
"DEFINED",
"CACHE",
"ENV",
"IN_LIST",
"EXISTS",
"IS_NEWER_THAN",
"IS_DIRECTORY",
"IS_SYMLINK",
"IS_ABSOLUTE",
"MATCHES",
"LESS",
"GREATER",
"EQUAL",
"LESS_EQUAL",
"GREATER_EQUAL",
"STRLESS",
"STRGREATER",
"STREQUAL",
"STRLESS_EQUAL",
"STRGREATER_EQUAL",
"VERSION_LESS",
"VERSION_GREATER",
"VERSION_EQUAL",
"VERSION_LESS_EQUAL",
"VERSION_GREATER_EQUAL",
];
foreach_args = ["IN", "RANGE", "ZIP_LISTS", "LISTS", "ITEMS"];
module.exports = grammar({
name: "cmake",
@ -5,9 +52,9 @@ module.exports = grammar({
source_file: ($) => repeat($._command_invocation),
escape_sequence: ($) => choice($._escape_identity, $._escape_encoded, $._escape_semicolon),
_escape_identity: ($) => /\\[^A-Za-z0-9;]/,
_escape_encoded: ($) => choice("\\t", "\\r", "\\n"),
_escape_semicolon: ($) => ";",
_escape_identity: (_) => /\\[^A-Za-z0-9;]/,
_escape_encoded: (_) => choice("\\t", "\\r", "\\n"),
_escape_semicolon: (_) => ";",
variable: ($) => prec.left(repeat1(choice(/[a-zA-Z0-9/_.+-]/, $.escape_sequence))),
variable_ref: ($) => choice($.normal_var, $.env_var, $.cache_var),
@ -27,16 +74,23 @@ module.exports = grammar({
unquoted_argument: ($) => repeat1(choice($.variable_ref, /[^ ()#\"\\]/, $.escape_sequence)),
foreach_command: ($) => command($.foreach, args(choice($.argument, "IN", "RANGE", "ZIP_LISTS", "LISTS", "ITEMS"))),
if_command: ($) => command($.if, args(choice($.argument, ...if_args))),
elseif_command: ($) => command($.elseif, args(choice($.argument, ...if_args))),
else_command: ($) => command($.else, optional(args(choice($.argument, ...if_args)))),
endif_command: ($) => command($.endif, optional(args(choice($.argument, ...if_args)))),
foreach_command: ($) => command($.foreach, args(choice($.argument, ...foreach_args))),
endforeach_command: ($) => command($.endforeach, optional($.argument)),
foreach_loop: ($) => seq($.foreach_command, repeat($._command_invocation), $.endforeach_command),
normal_command: ($) => command($.identifier, optional(args($.argument))),
_command_invocation: ($) => choice($.normal_command, $.foreach_loop),
foreach_loop: ($) => seq($.foreach_command, repeat($._command_invocation), $.endforeach_command),
if_condition: ($) =>
seq($.if_command, repeat(choice($._command_invocation, $.elseif_command, $.else_command)), $.endif_command),
...commandNames("foreach", "endforeach"),
identifier: ($) => /[A-Za-z_][A-Za-z0-9_]*/,
integer: ($) => /[+-]*\d+/,
_command_invocation: ($) => choice($.normal_command, $.if_condition, $.foreach_loop),
...commandNames("if", "elseif", "else", "endif", "foreach", "endforeach"),
identifier: (_) => /[A-Za-z_][A-Za-z0-9_]*/,
integer: (_) => /[+-]*\d+/,
},
});

File diff suppressed because it is too large Load diff

View file

@ -57,6 +57,44 @@
]
}
},
{
"type": "else_command",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "argument",
"named": true
},
{
"type": "else",
"named": true
}
]
}
},
{
"type": "elseif_command",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "argument",
"named": true
},
{
"type": "elseif",
"named": true
}
]
}
},
{
"type": "endforeach_command",
"named": true,
@ -76,6 +114,25 @@
]
}
},
{
"type": "endif_command",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "argument",
"named": true
},
{
"type": "endif",
"named": true
}
]
}
},
{
"type": "env_var",
"named": true,
@ -135,6 +192,68 @@
"type": "foreach_loop",
"named": true
},
{
"type": "if_condition",
"named": true
},
{
"type": "normal_command",
"named": true
}
]
}
},
{
"type": "if_command",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "argument",
"named": true
},
{
"type": "if",
"named": true
}
]
}
},
{
"type": "if_condition",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "else_command",
"named": true
},
{
"type": "elseif_command",
"named": true
},
{
"type": "endif_command",
"named": true
},
{
"type": "foreach_loop",
"named": true
},
{
"type": "if_command",
"named": true
},
{
"type": "if_condition",
"named": true
},
{
"type": "normal_command",
"named": true
@ -222,6 +341,10 @@
"type": "foreach_loop",
"named": true
},
{
"type": "if_condition",
"named": true
},
{
"type": "normal_command",
"named": true
@ -310,26 +433,202 @@
"type": ")",
"named": false
},
{
"type": "0",
"named": false
},
{
"type": "1",
"named": false
},
{
"type": "=",
"named": false
},
{
"type": "AND",
"named": false
},
{
"type": "CACHE",
"named": false
},
{
"type": "COMMAND",
"named": false
},
{
"type": "DEFINED",
"named": false
},
{
"type": "ENV",
"named": false
},
{
"type": "EQUAL",
"named": false
},
{
"type": "EXISTS",
"named": false
},
{
"type": "FALSE",
"named": false
},
{
"type": "GREATER",
"named": false
},
{
"type": "GREATER_EQUAL",
"named": false
},
{
"type": "IGNORE",
"named": false
},
{
"type": "IN",
"named": false
},
{
"type": "IN_LIST",
"named": false
},
{
"type": "IS_ABSOLUTE",
"named": false
},
{
"type": "IS_DIRECTORY",
"named": false
},
{
"type": "IS_NEWER_THAN",
"named": false
},
{
"type": "IS_SYMLINK",
"named": false
},
{
"type": "ITEMS",
"named": false
},
{
"type": "LESS",
"named": false
},
{
"type": "LESS_EQUAL",
"named": false
},
{
"type": "LISTS",
"named": false
},
{
"type": "MATCHES",
"named": false
},
{
"type": "N",
"named": false
},
{
"type": "NO",
"named": false
},
{
"type": "NOT",
"named": false
},
{
"type": "NOTFOUND",
"named": false
},
{
"type": "OFF",
"named": false
},
{
"type": "ON",
"named": false
},
{
"type": "OR",
"named": false
},
{
"type": "POLICY",
"named": false
},
{
"type": "RANGE",
"named": false
},
{
"type": "STREQUAL",
"named": false
},
{
"type": "STRGREATER",
"named": false
},
{
"type": "STRGREATER_EQUAL",
"named": false
},
{
"type": "STRLESS",
"named": false
},
{
"type": "STRLESS_EQUAL",
"named": false
},
{
"type": "TARGET",
"named": false
},
{
"type": "TEST",
"named": false
},
{
"type": "TRUE",
"named": false
},
{
"type": "VERSION_EQUAL",
"named": false
},
{
"type": "VERSION_GREATER",
"named": false
},
{
"type": "VERSION_GREATER_EQUAL",
"named": false
},
{
"type": "VERSION_LESS",
"named": false
},
{
"type": "VERSION_LESS_EQUAL",
"named": false
},
{
"type": "Y",
"named": false
},
{
"type": "YES",
"named": false
},
{
"type": "ZIP_LISTS",
"named": false
@ -358,10 +657,22 @@
"type": "]",
"named": false
},
{
"type": "else",
"named": true
},
{
"type": "elseif",
"named": true
},
{
"type": "endforeach",
"named": true
},
{
"type": "endif",
"named": true
},
{
"type": "foreach",
"named": true
@ -370,6 +681,10 @@
"type": "identifier",
"named": true
},
{
"type": "if",
"named": true
},
{
"type": "{",
"named": false

File diff suppressed because it is too large Load diff