| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''Module for parsing shell-like commands with logical operators.''' |
| 5 |
|
| 6 |
from .string_stream import StringStream |
| 7 |
|
| 8 |
|
| 9 |
# pylint: disable=too-few-public-methods |
| 10 |
|
| 11 |
|
| 12 |
class Literal: |
| 13 |
'''Represents a literal string.''' |
| 14 |
|
| 15 |
def __init__(self, string): |
| 16 |
self.string = string |
| 17 |
|
| 18 |
def __str__(self): |
| 19 |
return self.string |
| 20 |
|
| 21 |
def __repr__(self): |
| 22 |
return f'Literal({self.string!r})' |
| 23 |
|
| 24 |
|
| 25 |
class Command: |
| 26 |
'''Represents a command.''' |
| 27 |
|
| 28 |
def __init__(self): |
| 29 |
self.args = [] |
| 30 |
|
| 31 |
def __repr__(self): |
| 32 |
return f'Command({self.args!r})' |
| 33 |
|
| 34 |
|
| 35 |
class Lexer(StringStream): |
| 36 |
'''Lexer class.''' |
| 37 |
|
| 38 |
def parse(self): |
| 39 |
'''Split string into tokens (e.g. logical operators) and literals.''' |
| 40 |
|
| 41 |
tokens = [] |
| 42 |
while True: |
| 43 |
token = self._parse_token() |
| 44 |
if token is not None: |
| 45 |
tokens.append(token) |
| 46 |
else: |
| 47 |
return tokens |
| 48 |
|
| 49 |
def _parse_token(self): |
| 50 |
c = self.peek() |
| 51 |
|
| 52 |
if c is None: |
| 53 |
return None |
| 54 |
|
| 55 |
if c in ('!', '(', ')'): |
| 56 |
self.advance(1) |
| 57 |
return c |
| 58 |
|
| 59 |
if c.isspace(): |
| 60 |
self.advance(1) |
| 61 |
return self._parse_token() |
| 62 |
|
| 63 |
if c == '&': |
| 64 |
if not self.peek(1) == '&': |
| 65 |
raise ValueError('Single `&` found') |
| 66 |
self.advance(2) |
| 67 |
return '&&' |
| 68 |
|
| 69 |
if c == '|': |
| 70 |
if not self.peek(1) == '|': |
| 71 |
raise ValueError('Single `|` found') |
| 72 |
self.advance(2) |
| 73 |
return '||' |
| 74 |
|
| 75 |
return self._parse_literal() |
| 76 |
|
| 77 |
def _parse_literal(self): |
| 78 |
literal = '' |
| 79 |
|
| 80 |
while True: |
| 81 |
c = self.peek() |
| 82 |
|
| 83 |
if c is None: |
| 84 |
return Literal(literal) |
| 85 |
if c == '"': |
| 86 |
literal += self.parse_shell_double_quote(in_quotes=False) |
| 87 |
elif c == "'": |
| 88 |
literal += self.parse_shell_single_quote(in_quotes=False) |
| 89 |
elif c.isspace() or c in ('&', '|', '!', '(', ')'): |
| 90 |
return Literal(literal) |
| 91 |
else: |
| 92 |
self.advance(1) |
| 93 |
literal += c |
| 94 |
|
| 95 |
|
| 96 |
def make_commands(tokens): |
| 97 |
'''Parse tokens. |
| 98 |
|
| 99 |
Input: |
| 100 |
[Literal("foo"), Literal("bar"), '&&', Literal("baz")] |
| 101 |
|
| 102 |
Output: |
| 103 |
[Command(["foo", "bar"]), '&&', Command(["baz"])] |
| 104 |
''' |
| 105 |
|
| 106 |
new_tokens = [] |
| 107 |
current = Command() |
| 108 |
|
| 109 |
for token in tokens: |
| 110 |
if token in ('&&', '||', '!', '(', ')'): |
| 111 |
if current.args: |
| 112 |
new_tokens.append(current) |
| 113 |
current = Command() |
| 114 |
new_tokens.append(token) |
| 115 |
else: |
| 116 |
current.args.append(str(token)) |
| 117 |
|
| 118 |
if current.args: |
| 119 |
new_tokens.append(current) |
| 120 |
|
| 121 |
return new_tokens |
| 122 |
|
| 123 |
|
| 124 |
def check_syntax(tokens): |
| 125 |
'''Checks tokens for syntax errors.''' |
| 126 |
|
| 127 |
last = None |
| 128 |
parentheses = 0 |
| 129 |
|
| 130 |
for token in tokens: |
| 131 |
if token == '(': |
| 132 |
parentheses += 1 |
| 133 |
if last not in (None, '&&', '||', '!', '('): |
| 134 |
raise ValueError("Unexpected `(`") |
| 135 |
|
| 136 |
elif token == ')': |
| 137 |
parentheses -= 1 |
| 138 |
if parentheses < 0 or last in (None, '(', '&&', '||', '!'): |
| 139 |
raise ValueError("Unexpected `)`") |
| 140 |
|
| 141 |
elif token in ('&&', '||'): |
| 142 |
if last in (None, '(', '&&', '||', '!'): |
| 143 |
raise ValueError(f"Unexpected `{token}`") |
| 144 |
|
| 145 |
elif token == '!': |
| 146 |
if last not in (None, '(', '&&', '||', '!'): |
| 147 |
raise ValueError("Unexpected `!`") |
| 148 |
|
| 149 |
last = token |
| 150 |
|
| 151 |
if parentheses > 0: |
| 152 |
raise ValueError("Unclosed `(`") |
| 153 |
|
| 154 |
if last is None: |
| 155 |
raise ValueError("No command found") |
| 156 |
|
| 157 |
|
| 158 |
def parse(string): |
| 159 |
'''Parse a string and turn it into And/Or/Not/Command objects.''' |
| 160 |
|
| 161 |
lex_tokens = Lexer(string).parse() |
| 162 |
tokens = make_commands(lex_tokens) |
| 163 |
check_syntax(tokens) |
| 164 |
return tokens |