mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-25 10:17:02 -06:00
VectorCraft does not run scripts written for the reference app, and File › Scripts held only the disabled Other Script…. File › Scripts › Check Script… picks a .jsx, .jsxinc or .jsxbin file, reads it without running any of it, and shows a report: what the script needs (the script language's own objects, the document model, files, menu commands, dialogs, or features only the reference app has), what it would do outside the document, the menu-command strings it passes, the document-model member chains and type names it uses, its directives, the files it includes, and the problems that would stop it loading. Copy Report copies the report as JSON. File › Open and drag-and-drop show the report for those files too. - vectorcraft-scriptcheck, a new Standalone crate with no dependencies, holds the checker: a lexer for ECMAScript 3 with E4X literals and the #name and //@name directive lines, and an analysis of the tokens that follows variables, function scopes and the files a script includes. It never recurses: open brackets, the levels of an XML literal and the function bodies being read are kept on stacks capped at 4,096. It reads at most 8 MiB of a file and 32 MiB with the included files, follows 64 included files 16 deep, looks up at most 1,024 paths for them, keeps 1,000 entries in a list, tracks 1,000 variables holding member chains and 1,000 holding File objects, and cuts echoed text to 120 characters. A compiled script is reported as compiled and isn't read. - script.check (engine, a query) takes path, data or dataBase64. With a path and includes: true, it reads the files the #include lines name from inside the script's folder, through the automation read root, and when the script or one of those files assigns a member chain or a File object to a variable, the check treats the variable as holding it in every file. It doesn't follow absolute names, names, folders or links that lead out of the folder, names or folders longer than 4,096 bytes, or JSINCLUDE, and it reads only regular files. - ui.checkScript shows the report (dialog scriptCheck). File › Open lists Scripts (.jsx, .jsxinc, .jsxbin) and sends a script's path to script.check, which reads at most 8 MiB of it. The language rules come from ECMA-262 3rd edition and ECMA-357 2nd edition. The directive, platform and document-model root names come from the public JavaScript Tools Guide and scripting guide pages that catalog.rs cites by URL, and executeMenuCommand, doScript, loadAction, unloadAction and selectTool come from the public forum posts cited there; only names are used. Every script in the tests is written from scratch with invented names. Docs: docs/mcp.md, docs/target-app-parity.md.
88 lines
3.3 KiB
Rust
88 lines
3.3 KiB
Rust
//! Preprocessor lines (`#target acme-12`, `//@include "lib.jsxinc"`): their names and values, the
|
|
//! parts of a target's application specifier, and the folders an `#includepath` line names.
|
|
|
|
use crate::catalog;
|
|
|
|
/// A preprocessor line.
|
|
pub(crate) struct Line<'s> {
|
|
/// `#` or `//@`.
|
|
pub form: &'static str,
|
|
/// The name as written.
|
|
pub name: &'s str,
|
|
/// The documented directive the name is (compared without case), if it is one.
|
|
pub known: Option<&'static str>,
|
|
/// Its value: the text after the name, without quotes around it, or else without a trailing
|
|
/// `//` comment and `;`.
|
|
pub value: &'s str,
|
|
}
|
|
|
|
/// The line `text`, which starts with `#` or `//@`.
|
|
pub(crate) fn parse(text: &str) -> Line<'_> {
|
|
let (form, rest) = match text.strip_prefix("//@") {
|
|
Some(rest) => ("//@", rest),
|
|
None => ("#", text.strip_prefix('#').unwrap_or(text)),
|
|
};
|
|
let end = rest.find(|c: char| !c.is_ascii_alphanumeric() && c != '_').unwrap_or(rest.len());
|
|
let (name, rest) = (rest.get(..end).unwrap_or(rest), rest.get(end..).unwrap_or_default());
|
|
let known = catalog::DIRECTIVES.iter().copied().find(|d| d.eq_ignore_ascii_case(name));
|
|
Line { form, name, known, value: value(rest) }
|
|
}
|
|
|
|
fn value(rest: &str) -> &str {
|
|
let v = rest.trim();
|
|
if let Some(quote) = v.chars().next().filter(|c| matches!(c, '"' | '\'')) {
|
|
let inner = v.get(1..).unwrap_or_default();
|
|
return inner.find(quote).and_then(|i| inner.get(..i)).unwrap_or(inner);
|
|
}
|
|
let v = v.find("//").and_then(|i| v.get(..i)).unwrap_or(v);
|
|
v.trim_end().trim_end_matches(';').trim_end()
|
|
}
|
|
|
|
/// The parts of an application specifier, `name_instance-version-locale`, every part after the name
|
|
/// optional, as
|
|
/// https://extendscript.docsforadobe.dev/interapplication-communication/application-and-namespace-specifiers/
|
|
/// lays it out.
|
|
pub(crate) struct Specifier<'s> {
|
|
pub app: &'s str,
|
|
pub instance: Option<&'s str>,
|
|
pub version: Option<&'s str>,
|
|
pub locale: Option<&'s str>,
|
|
}
|
|
|
|
pub(crate) fn target(value: &str) -> Specifier<'_> {
|
|
let (head, tail) = match value.split_once('-') {
|
|
Some((head, tail)) => (head, Some(tail)),
|
|
None => (value, None),
|
|
};
|
|
let (app, instance) = match head.split_once('_') {
|
|
Some((app, instance)) => (app, Some(instance)),
|
|
None => (head, None),
|
|
};
|
|
let (version, locale) = match tail.map(|t| t.split_once('-').map_or((t, None), |(v, l)| (v, Some(l)))) {
|
|
Some((version, locale)) => (Some(version), locale),
|
|
None => (None, None),
|
|
};
|
|
fn part(s: Option<&str>) -> Option<&str> {
|
|
s.filter(|s| !s.is_empty())
|
|
}
|
|
Specifier { app, instance: part(instance), version: part(version), locale: part(locale) }
|
|
}
|
|
|
|
/// The most folders an `#includepath` line names that are kept.
|
|
const MAX_PATHS: usize = 64;
|
|
|
|
/// The longest `#include` name or `#includepath` folder followed, in bytes.
|
|
pub(crate) const MAX_PATH: usize = 4096;
|
|
|
|
/// The folders an `#includepath` value names, separated by `;`: the first [`MAX_PATHS`] of those
|
|
/// no longer than [`MAX_PATH`].
|
|
pub(crate) fn include_paths(value: &str) -> Vec<String> {
|
|
value
|
|
.split(';')
|
|
.map(|p| p.trim().trim_matches(['"', '\'']))
|
|
.filter(|p| !p.is_empty() && p.len() <= MAX_PATH)
|
|
.take(MAX_PATHS)
|
|
.map(str::to_string)
|
|
.collect()
|
|
}
|