Revert the old syntax parser to before macros

The macro syntax in the old parser is not needed.
This commit is contained in:
Jules Aguillon 2025-02-22 12:25:40 +01:00
parent 8d90e3c4d2
commit 47d7250b90

View File

@ -141,51 +141,18 @@ public final class KeyValueParser
static Pattern QUOTED_PAT; static Pattern QUOTED_PAT;
static Pattern PAYLOAD_START_PAT; static Pattern PAYLOAD_START_PAT;
static Pattern WORD_PAT; static Pattern WORD_PAT;
static Pattern COMMA_PAT;
static Pattern END_OF_INPUT_PAT;
static public KeyValue parse(String str) throws ParseError static public KeyValue parse(String str) throws ParseError
{ {
init();
Matcher m = START_PAT.matcher(str);
KeyValue k = parseKeyValue(m);
if (!match(m, END_OF_INPUT_PAT))
parseError("Unexpected character", m);
return k;
}
static void init()
{
if (START_PAT != null)
return;
START_PAT = Pattern.compile(":(\\w+)");
ATTR_PAT = Pattern.compile("\\s*(\\w+)\\s*=");
QUOTED_PAT = Pattern.compile("'(([^'\\\\]+|\\\\')*)'");
PAYLOAD_START_PAT = Pattern.compile("\\s*:");
WORD_PAT = Pattern.compile("[a-zA-Z0-9_]+|.");
COMMA_PAT = Pattern.compile(",");
END_OF_INPUT_PAT = Pattern.compile("$");
}
static KeyValue parseKeyValue(Matcher m) throws ParseError
{
if (match(m, START_PAT))
return parseComplexKeyValue(m, m.group(1));
// Key doesn't start with ':', accept either a char key or a key name.
if (!match(m, WORD_PAT))
parseError("Expected key, for example \":str ...\".", m);
String key = m.group(0);
KeyValue k = KeyValue.getSpecialKeyByName(key);
if (k == null)
return KeyValue.makeStringKey(key);
return k;
}
static KeyValue parseComplexKeyValue(Matcher m, String kind) throws ParseError
{
// Attributes
String symbol = null; String symbol = null;
int flags = 0; int flags = 0;
init();
// Kind
Matcher m = START_PAT.matcher(str);
if (!m.lookingAt())
parseError("Expected kind, for example \":str ...\".", m);
String kind = m.group(1);
// Attributes
while (true) while (true)
{ {
if (!match(m, ATTR_PAT)) if (!match(m, ATTR_PAT))
@ -233,14 +200,6 @@ public final class KeyValueParser
symbol = String.valueOf(eventcode); symbol = String.valueOf(eventcode);
return KeyValue.keyeventKey(symbol, eventcode, flags); return KeyValue.keyeventKey(symbol, eventcode, flags);
case "macro":
// :macro symbol='copy':ctrl,a,ctrl,c
// :macro symbol='acute':compose,'
KeyValue[] macro = parseKeyValueList(m);
if (symbol == null)
symbol = "macro";
return KeyValue.makeMacro(symbol, macro, flags);
default: break; default: break;
} }
parseError("Unknown kind '"+kind+"'", m, 1); parseError("Unknown kind '"+kind+"'", m, 1);
@ -276,14 +235,22 @@ public final class KeyValueParser
return flags; return flags;
} }
// Parse keys separated by comas static boolean match(Matcher m, Pattern pat)
static KeyValue[] parseKeyValueList(Matcher m) throws ParseError
{ {
ArrayList<KeyValue> out = new ArrayList<KeyValue>(); try { m.region(m.end(), m.regionEnd()); } catch (Exception _e) {}
out.add(parseKeyValue(m)); m.usePattern(pat);
while (match(m, COMMA_PAT)) return m.lookingAt();
out.add(parseKeyValue(m)); }
return out.toArray(new KeyValue[]{});
static void init()
{
if (START_PAT != null)
return;
START_PAT = Pattern.compile(":(\\w+)");
ATTR_PAT = Pattern.compile("\\s*(\\w+)\\s*=");
QUOTED_PAT = Pattern.compile("'(([^'\\\\]+|\\\\')*)'");
PAYLOAD_START_PAT = Pattern.compile("\\s*:");
WORD_PAT = Pattern.compile("[a-zA-Z0-9_]*");
} }
} }