scratchblocks: rótulos com ( ) e vírgula ("nova imagem(96 x 72)", "em (x,y)")
Rótulos de library com pontuação literal não casavam: o "(" do rótulo
virava campo da notação. Agora, nos rótulos do editor e das libraries,
"( )" e "," são texto, e na importação um grupo ( ) com vários tokens
dentro é aberto quando nada casa. Aceita o que o exportador escreve
("nova imagem((96) x (72))") e a forma sem parênteses extras; a linha
"pinte a imagem ... em ((px),(py))" sumia sem aviso.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
63c8fb0c1c
commit
feb20a2127
2 files changed
+77
-6
No files matched your search
@@ -264,12 +264,27 @@ static int slot_ref(const char* s, bool* list) {
|
||||
return *p ? -1 : v - 1;
|
||||
}
|
||||
|
||||
static bool model_build(Model* m, const char* type, const char* text, char shape) {
|
||||
/* Rotulo de library/editor: "( )" e "," sao texto, nao campos da notacao
|
||||
* ("nova imagem(%1 x %2)" -> "nova imagem %1 x %2", "em (%2,%3)" -> "em %2 , %3") */
|
||||
static void loose_punct(char* buf, size_t cap) {
|
||||
char out[512];
|
||||
size_t n = 0;
|
||||
for (const char* p = buf; *p && n + 3 < sizeof(out); p++) {
|
||||
if (*p == '(' || *p == ')') out[n++] = ' ';
|
||||
else if (*p == ',') { out[n++] = ' '; out[n++] = ','; out[n++] = ' '; }
|
||||
else out[n++] = *p;
|
||||
}
|
||||
out[n] = 0;
|
||||
snprintf(buf, cap, "%s", out);
|
||||
}
|
||||
|
||||
static bool model_build(Model* m, const char* type, const char* text, char shape, bool loose) {
|
||||
memset(m, 0, sizeof(*m));
|
||||
m->type = type;
|
||||
m->shape = shape;
|
||||
char buf[512];
|
||||
snprintf(buf, sizeof(buf), "%s", text);
|
||||
if (loose) loose_punct(buf, sizeof(buf));
|
||||
const char* cc = strstr(text, "::");
|
||||
if (cc) {
|
||||
cc += 2;
|
||||
@@ -304,7 +319,7 @@ static Models models_load(const TbSbContext* cx) {
|
||||
if (!M.m) return M;
|
||||
for (int i = 0; i < BUILTIN_N; i++) {
|
||||
Model* m = &M.m[M.n];
|
||||
if (!model_build(m, s_builtin[i].type, s_builtin[i].text, s_builtin[i].shape)) continue;
|
||||
if (!model_build(m, s_builtin[i].type, s_builtin[i].text, s_builtin[i].shape, false)) continue;
|
||||
m->builtin = true;
|
||||
m->import_only = s_builtin[i].import_only;
|
||||
M.n++;
|
||||
@@ -314,7 +329,7 @@ static Models models_load(const TbSbContext* cx) {
|
||||
if (!L->type || !L->label) continue;
|
||||
int sh = cx_shape(cx, L->type);
|
||||
Model* m = &M.m[M.n];
|
||||
if (!model_build(m, L->type, L->label, (char)shape_char(sh < 0 ? TB_SB_STACK : sh))) continue;
|
||||
if (!model_build(m, L->type, L->label, (char)shape_char(sh < 0 ? TB_SB_STACK : sh), true)) continue;
|
||||
/* "%1" sozinho (ex.: valor de variavel) casaria com qualquer coisa */
|
||||
if (m->n == 1 && m->slot[0] >= 0) continue;
|
||||
M.n++;
|
||||
@@ -604,6 +619,34 @@ static const Model* find_model(const Models* M, const Tok* t, int n, char cls) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Nenhum modelo casou: abre os grupos ( ) que tem varios tokens dentro
|
||||
* ("imagem((96) x (72))" -> imagem (96) x (72)) e tenta de novo. E como a
|
||||
* pontuacao literal dos rotulos de library volta ao formato dos modelos. */
|
||||
static const Model* find_model_loose(const Models* M, Tok* t, int* n, char cls) {
|
||||
const Model* m = find_model(M, t, *n, cls);
|
||||
if (m) return m;
|
||||
Tok flat[TOK_MAX];
|
||||
int fn = 0;
|
||||
bool changed = false;
|
||||
for (int i = 0; i < *n && fn < TOK_MAX; i++) {
|
||||
Tok inner[TOK_MAX];
|
||||
int k = t[i].kind == T_ROUND ? tokenize(t[i].text, inner, TOK_MAX) : -1;
|
||||
if (k > 1) {
|
||||
for (int j = 0; j < k && fn < TOK_MAX; j++) flat[fn++] = inner[j];
|
||||
changed = true;
|
||||
} else {
|
||||
flat[fn++] = t[i];
|
||||
}
|
||||
}
|
||||
if (!changed) return NULL;
|
||||
m = find_model(M, flat, fn, cls);
|
||||
if (m) {
|
||||
memcpy(t, flat, (size_t)fn * sizeof(Tok));
|
||||
*n = fn;
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
/* Conteudo de um ( ) ou < > -> repórter */
|
||||
static int parse_inner(Imp* I, const char* text, char cls) {
|
||||
char buf[TOK_TEXT];
|
||||
@@ -612,7 +655,7 @@ static int parse_inner(Imp* I, const char* text, char cls) {
|
||||
Tok t[TOK_MAX];
|
||||
int n = tokenize(buf, t, TOK_MAX);
|
||||
if (n < 0) { imp_warn(I, "parenteses nao fecham", text); return -1; }
|
||||
const Model* m = find_model(I->M, t, n, cls);
|
||||
const Model* m = find_model_loose(I->M, t, &n, cls);
|
||||
if (m) return build_from(I, m, t, n);
|
||||
bool words = n > 0;
|
||||
for (int i = 0; i < n; i++) if (t[i].kind != T_WORD) words = false;
|
||||
@@ -702,7 +745,7 @@ int tb_scratchblocks_import(const char* text, TbBpTab* out, const TbSbContext* c
|
||||
Tok t[TOK_MAX];
|
||||
int n = tokenize(body, t, TOK_MAX);
|
||||
if (n < 0) { imp_warn(&I, "parenteses nao fecham", line); continue; }
|
||||
m = find_model(&M, t, n, 's');
|
||||
m = find_model_loose(&M, t, &n, 's');
|
||||
if (!m) { imp_warn(&I, "bloco desconhecido", line); continue; }
|
||||
bi = build_from(&I, m, t, n);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user