Expr's rewrite step moves to ExprRewriter; Builder's tokenizer moves to RuleTokens and its value list to BuilderValues; BoardTester's static checks move to BoardChecks and its report text to BoardReportText. The original classes keep one-line delegators, so no caller changes. Expr's dice regex and word test now reuse RuleTokens (one copy). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
153 lines
4.9 KiB
GDScript
153 lines
4.9 KiB
GDScript
class_name RuleTokens
|
|
extends RefCounted
|
|
## Splits rule text into small pieces ("tokens") such as @luck, >=, 8 or
|
|
## "Aslan", so the Builder can read conditions and effects as rows. Also
|
|
## holds the small text helpers that go with it: is a character part of a
|
|
## word, dice words like 2d6, and quoting text. Builder delegates here.
|
|
|
|
static var _dice_re: RegEx = null
|
|
static var _num_re: RegEx = null
|
|
|
|
|
|
## Splits a rule into tokens: Array of {"t": type, "s": text}, or [] when it
|
|
## can't (an unclosed quote or bracket, a stray character, "2x"). Types:
|
|
## "var" (@name, lowercased, or @[expr] as written), "prefix" (@name_*),
|
|
## "name" (a chain name or function), "num", "dice" (2d6, D3, 2D), "str" (a
|
|
## quoted text, quotes included), "op" (+ - * / % ( ) , = == != < <= > >=),
|
|
## "kw" (and, or, not; && || ! are read as these).
|
|
static func tokenize(src: String) -> Array:
|
|
var out: Array = []
|
|
var i := 0
|
|
var src_len := src.length()
|
|
while i < src_len:
|
|
var ch := src[i]
|
|
if ch == " " or ch == "\t" or ch == "\n":
|
|
i += 1
|
|
continue
|
|
if ch == "\"" or ch == "'":
|
|
var j := i + 1
|
|
while j < src_len and src[j] != ch:
|
|
if src[j] == "\\":
|
|
j += 1
|
|
j += 1
|
|
if j >= src_len:
|
|
return []
|
|
out.append({"t": "str", "s": src.substr(i, j - i + 1)})
|
|
i = j + 1
|
|
continue
|
|
if ch == "@":
|
|
if i + 1 < src_len and src[i + 1] == "[":
|
|
var close := Expr.bracket_end(src, i + 1)
|
|
if close < 0:
|
|
return []
|
|
out.append({"t": "var", "s": "@[%s]" % src.substr(i + 2, close - i - 2).strip_edges()})
|
|
i = close + 1
|
|
continue
|
|
var name_end := i + 1
|
|
while name_end < src_len and is_word(src[name_end]):
|
|
name_end += 1
|
|
var name := src.substr(i + 1, name_end - i - 1).to_lower()
|
|
if name == "" or name[0].is_valid_int():
|
|
return []
|
|
if name_end < src_len and src[name_end] == "*":
|
|
out.append({"t": "prefix", "s": "@%s*" % name})
|
|
i = name_end + 1
|
|
else:
|
|
out.append({"t": "var", "s": "@" + name})
|
|
i = name_end
|
|
continue
|
|
if is_word(ch):
|
|
var word_end := i
|
|
while word_end < src_len and (is_word(src[word_end]) or (src[word_end] == "." and word_end + 1 < src_len and src[word_end + 1].is_valid_int())):
|
|
word_end += 1
|
|
var word := src.substr(i, word_end - i)
|
|
i = word_end
|
|
if _num().search(word) != null:
|
|
out.append({"t": "num", "s": word})
|
|
elif dice_regex().search(word) != null and word.length() > 1:
|
|
out.append({"t": "dice", "s": word})
|
|
elif word[0].is_valid_int():
|
|
return []
|
|
elif ["and", "or", "not"].has(word.to_lower()) and word == word.to_lower():
|
|
out.append({"t": "kw", "s": word})
|
|
else:
|
|
out.append({"t": "name", "s": word})
|
|
continue
|
|
var two := src.substr(i, 2)
|
|
if two in [">=", "<=", "!=", "=="]:
|
|
out.append({"t": "op", "s": two})
|
|
i += 2
|
|
continue
|
|
if two == "&&" or two == "||":
|
|
out.append({"t": "kw", "s": "and" if two == "&&" else "or"})
|
|
i += 2
|
|
continue
|
|
if ch == "!":
|
|
out.append({"t": "kw", "s": "not"})
|
|
i += 1
|
|
continue
|
|
if ch in ["+", "-", "*", "/", "%", "(", ")", ",", "=", "<", ">"]:
|
|
out.append({"t": "op", "s": ch})
|
|
i += 1
|
|
continue
|
|
return []
|
|
return out
|
|
|
|
|
|
## True when `ch` can be part of a name or number: a letter, a digit or "_".
|
|
static func is_word(ch: String) -> bool:
|
|
return ch == "_" or ch.is_valid_int() or (ch.to_lower() != ch.to_upper())
|
|
|
|
|
|
## The pattern for a dice word: an optional count, d or D, optional sides.
|
|
static func dice_regex() -> RegEx:
|
|
if _dice_re == null:
|
|
_dice_re = RegEx.create_from_string("^(\\d*)[dD](\\d*)$")
|
|
return _dice_re
|
|
|
|
|
|
static func _num() -> RegEx:
|
|
if _num_re == null:
|
|
_num_re = RegEx.create_from_string("^\\d+(\\.\\d+)?$")
|
|
return _num_re
|
|
|
|
|
|
## [count, sides] of a dice token ("2d6" -> [2, 6], "D3" -> [1, 3], "2D" ->
|
|
## [2, 6]), or [].
|
|
static func dice_parts(word: String) -> Array:
|
|
var found := dice_regex().search(word)
|
|
if found == null or word.length() < 2:
|
|
return []
|
|
var count_text := found.get_string(1)
|
|
var sides_text := found.get_string(2)
|
|
return [int(count_text) if count_text != "" else 1, int(sides_text) if sides_text != "" else 6]
|
|
|
|
|
|
## True when `text` is one quoted text literal ("Aslan" or 'Aslan').
|
|
static func is_text_literal(text: String) -> bool:
|
|
var toks := tokenize(text.strip_edges())
|
|
return toks.size() == 1 and toks[0]["t"] == "str"
|
|
|
|
|
|
## A quoted literal's text: "\"Aslan\"" -> "Aslan" (\" and \\ unescaped).
|
|
## Anything else comes back as it is.
|
|
static func unquote(text: String) -> String:
|
|
text = text.strip_edges()
|
|
if text.length() < 2 or not ((text.begins_with("\"") and text.ends_with("\"")) or (text.begins_with("'") and text.ends_with("'"))):
|
|
return text
|
|
var inner := text.substr(1, text.length() - 2)
|
|
var out := ""
|
|
var pos := 0
|
|
while pos < inner.length():
|
|
var ch := inner[pos]
|
|
if ch == "\\" and pos + 1 < inner.length():
|
|
pos += 1
|
|
ch = inner[pos]
|
|
out += ch
|
|
pos += 1
|
|
return out
|
|
|
|
|
|
## Text as a double-quoted literal, escaping \ and ".
|
|
static func quote(text: String) -> String:
|
|
return "\"%s\"" % text.replace("\\", "\\\\").replace("\"", "\\\"")
|