Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import java.util.regex.Pattern;

/**
* Deterministic parser for confirmation prompt syntax (<c:...> / <confirm:...>).
* Deterministic parser for confirmation prompt syntax ({@code <c:...>}, {@code <confirm:...>}, or
* {@code <confirmation:...>}).
*
* <p>Validates and extracts prompt text, button labels, presentation mode override, value-mode
* flag, and sound keys according to the confirmation prompt grammar specification.
Expand All @@ -23,11 +24,13 @@ private ConfirmationGrammar() {}
* Checks whether the given key is a recognized confirmation prompt key (case-insensitive).
*
* @param key the prompt key to test
* @return true if key is "c" or "confirm", false otherwise
* @return true if key is a built-in confirmation alias, false otherwise
*/
public static boolean isConfirmationKey(String key) {
if (key == null) return false;
return key.equalsIgnoreCase("c") || key.equalsIgnoreCase("confirm");
return key.equalsIgnoreCase("c")
|| key.equalsIgnoreCase("confirm")
|| key.equalsIgnoreCase("confirmation");
}

/**
Expand All @@ -42,9 +45,10 @@ public static ConfirmationSyntax parse(PromptTag tag) {
Objects.requireNonNull(tag, "PromptTag cannot be null");
if (!isConfirmationKey(tag.key())) {
throw new IllegalArgumentException(
"PromptTag key is not a confirmation key ('c' or 'confirm'): " + tag.key());
"PromptTag key is not a confirmation key ('c', 'confirm', or 'confirmation'): "
+ tag.key());
}
return parse(tag.displayText());
return parse(tag.grammarSource());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static ItemSyntax parse(PromptTag tag) {
throw new IllegalArgumentException(
"PromptTag key is not an item key ('i' or 'item'): " + tag.key());
}
return parse(tag.displayText());
return parse(tag.grammarSource());
}

/**
Expand Down
14 changes: 14 additions & 0 deletions prompt-core/src/main/java/dev/cyr1en/promptcore/ParsedCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,20 @@ public List<PostCommandMeta> onCancelPCMs() {
return postCmds.stream().filter(PostCommandMeta::onCancel).toList();
}

/** Returns the primary command text between parsed tags, in source order. */
public List<String> primaryTemplateFragments() {
if (templateSpans.isEmpty()) return List.of(templateCommand);

var fragments = new ArrayList<String>(templateSpans.size() + 1);
int cursor = 0;
for (var span : templateSpans) {
fragments.add(unescape(rawTemplateCommand.substring(cursor, span.start()), parserConfig));
cursor = span.end();
}
fragments.add(unescape(rawTemplateCommand.substring(cursor), parserConfig));
return List.copyOf(fragments);
}

/**
* Build a "partial command" string suitable for Brigadier parsing at the current prompt position.
*
Expand Down
37 changes: 36 additions & 1 deletion prompt-core/src/main/java/dev/cyr1en/promptcore/PromptTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
* @param flags immutable map of custom trailing flag names to string values (e.g. {@code
* -rarity:rare})
* @param breakIf optional compiled condition to evaluate for early flow termination
* @param grammarSource tag content with parser flags removed and grammar escapes intact
*/
public record PromptTag(
String rawTag,
Expand All @@ -73,7 +74,8 @@ public record PromptTag(
TitleConfig title,
Integer timeout,
Map<String, String> flags,
Condition breakIf) {
Condition breakIf,
String grammarSource) {

/**
* Flag name ({@code -allowargs}) stored in {@link #flags()} when a prompt may be answered with
Expand All @@ -97,6 +99,7 @@ public enum AnswerType {
Objects.requireNonNull(key);
Objects.requireNonNull(displayText);
Objects.requireNonNull(type);
Objects.requireNonNull(grammarSource);
if (timeout != null && (timeout < 1 || timeout > 3600)) {
throw new IllegalArgumentException(
"Timeout must be between 1 and 3600 seconds, got: " + timeout);
Expand All @@ -105,6 +108,38 @@ public enum AnswerType {
flags = flags == null ? Map.of() : Map.copyOf(flags);
}

/** Constructor for callers that supply their own display text as grammar source. */
public PromptTag(
String rawTag,
String key,
String filter,
String displayText,
boolean sanitize,
String validatorAlias,
AnswerType type,
List<PromptTag> subTags,
boolean preset,
TitleConfig title,
Integer timeout,
Map<String, String> flags,
Condition breakIf) {
this(
rawTag,
key,
filter,
displayText,
sanitize,
validatorAlias,
type,
subTags,
preset,
title,
timeout,
flags,
breakIf,
displayText);
}

/**
* Convenience shortcut for a single-row tag (no sub-tags) with default sanitize=true, no
* validator, no type constraint, no title wrapper, no timeout, {@code preset = false}, and empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ public static CompiledTemplate compile(String source, TemplateSyntax syntax) {
return compile(source, syntax, (body, _) -> body, Set.of());
}

/** Compiles each fragment independently so template syntax cannot cross fragment boundaries. */
public static CompiledTemplate compileFragments(List<String> fragments, TemplateSyntax syntax) {
Objects.requireNonNull(syntax, "syntax must not be null");
var source = String.join("", fragments);
checkSourceLength(source);
var segments = new ArrayList<TemplateSegment>();
for (var fragment : fragments) {
segments.addAll(compile(fragment, syntax).segments());
}
return new CompiledTemplate(source, segments);
}

/**
* Compiles with a placeholder normalizer and additional single-pass literal escapes. Escaped
* literals remain typed so downstream compilers cannot reinterpret them as extension syntax.
Expand All @@ -67,14 +79,7 @@ public static CompiledTemplate compile(
throw new IllegalArgumentException("additional literal escapes must not be empty");
}

if (source.length() > TransformLimits.MAX_TEMPLATE_LENGTH) {
throw new TransformException(
TransformErrorCode.TEMPLATE_TOO_LONG,
"Template source length exceeds limit of "
+ TransformLimits.MAX_TEMPLATE_LENGTH
+ ": "
+ source.length());
}
checkSourceLength(source);

for (int i = 0; i < source.length(); i++) {
char c = source.charAt(i);
Expand Down Expand Up @@ -227,6 +232,17 @@ public static CompiledTemplate compile(
return new CompiledTemplate(source, segments);
}

private static void checkSourceLength(String source) {
if (source.length() > TransformLimits.MAX_TEMPLATE_LENGTH) {
throw new TransformException(
TransformErrorCode.TEMPLATE_TOO_LONG,
"Template source length exceeds limit of "
+ TransformLimits.MAX_TEMPLATE_LENGTH
+ ": "
+ source.length());
}
}

private static void flushLiteral(List<TemplateSegment> segments, StringBuilder literalBuffer) {
if (literalBuffer.length() == 0) return;
segments.add(new LiteralSegment(literalBuffer.toString()));
Expand Down
Loading
Loading