-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBashToken.gd
98 lines (70 loc) · 2.58 KB
/
BashToken.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
extends Object
class_name BashToken
var type: String
var value = null # just a few tokens can have a null value`. Even when it's useless, it is best for the error mesages
var metadata = null # metadata will be null for all tokens except for STRING because we need to know what quotes were used.
func _init(t: String, v, m = null):
type = t
value = v
metadata = m
func is_plain() -> bool:
return type == Tokens.PLAIN
func is_flag() -> bool:
return type == Tokens.FLAG or type == Tokens.LONG_FLAG
func is_negative_digit() -> bool:
if type == Tokens.FLAG:
if value.is_valid_integer():
return int("-" + value) < 0
else:
return false
return false
func is_flag_and_equals(name: String):
return is_flag() and value == name
func is_string() -> bool:
return type == Tokens.STRING
func is_word() -> bool:
return is_string() or is_plain()
func is_pipe() -> bool:
return type == Tokens.PIPE
func is_eoi() -> bool:
return type == Tokens.EOI
func is_descriptor() -> bool:
return type == Tokens.DESCRIPTOR
func is_descriptor_and_equals(number: int) -> bool:
return type == Tokens.DESCRIPTOR and value == number
func is_writing_redirection() -> bool:
return type == Tokens.WRITING_REDIRECTION
func is_append_writing_redirection() -> bool:
return type == Tokens.APPEND_WRITING_REDIRECTION
func is_reading_redirection() -> bool:
return type == Tokens.READING_REDIRECTION
func is_redirection() -> bool:
return is_writing_redirection() or is_append_writing_redirection() or is_reading_redirection()
func is_equal_sign() -> bool:
return type == Tokens.EQUALS
func is_variable() -> bool:
return type == Tokens.VARIABLE
func is_keyword() -> bool:
return type == Tokens.KEYWORD
func is_keyword_and_equals(keyword: String) -> bool:
return is_keyword() and value == keyword
# (n)>&(m)
func is_and() -> bool:
return type == Tokens.AND
func is_command_substitution() -> bool:
return type == Tokens.SUBSTITUTION
func is_semicolon() -> bool:
return type == Tokens.SEMICOLON
func is_newline() -> bool:
return type == Tokens.NL
func is_line_separator() -> bool:
return is_newline() or is_semicolon()
func is_valid_token_in_for_loop() -> bool:
return is_plain() or is_string() or is_variable() or is_command_substitution()
func equals(another: BashToken) -> bool:
if self == another: return true
return self.type == another.type \
and self.value == another.value \
and (self.metadata.hash() == another.metadata.hash() if self.metadata != null and another.metadata != null else true)
func _to_string():
return "[" + type + ":" + str(value) + ("(" + str(metadata) + ")" if metadata != null else "") + "]"