@yannosay/sinth 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/sinth.ts +69 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yannosay/sinth",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "A declarative language that compiles to pure HTML, CSS, and JavaScript.",
5
5
  "author": "Yannosay Productions",
6
6
  "license": "AGPL-3.0",
package/src/sinth.ts CHANGED
@@ -255,8 +255,12 @@ export class Lexer {
255
255
  if (ch === "/") { tokens.push(this.single(TT.OP_SLASH, loc)); continue; }
256
256
  if (ch === "<") { tokens.push(this.single(TT.OP_LT, loc)); continue; }
257
257
  if (ch === ">") { tokens.push(this.single(TT.OP_GT, loc)); continue; }
258
- if (ch === "!" && this.src[this.pos + 1] === "=") { this.adv(); tokens.push({ type: TT.OP_NEQ, value: "!=", loc }); continue; }
259
-
258
+ if (ch === "!" && this.src[this.pos + 1] === "=") {
259
+ this.adv(); // consume '!'
260
+ this.adv(); // consume '='
261
+ tokens.push({ type: TT.OP_NEQ, value: "!=", loc });
262
+ continue;
263
+ }
260
264
  if (ch === "{") {
261
265
  if (this.rawBlockPending) {
262
266
  this.rawBlockPending = false;
@@ -674,6 +678,16 @@ export class Parser {
674
678
  return { kind: "unary", op: "not", operand };
675
679
  }
676
680
 
681
+ // Parenthesized expression
682
+ if (this.check(TT.LPAREN)) {
683
+ this.consume(TT.LPAREN);
684
+ const expr = this.parseExpression();
685
+ if (!expr) throw new SinthError("Expected expression after '('", this.peek().loc);
686
+ if (!this.check(TT.RPAREN)) throw new SinthError("Expected ')'", this.peek().loc);
687
+ this.consume(TT.RPAREN);
688
+ return expr;
689
+ }
690
+
677
691
  // Literal
678
692
  if (this.check(TT.STRING) || this.check(TT.NUMBER) || this.check(TT.BOOL_TRUE) || this.check(TT.BOOL_FALSE) || this.check(TT.NULL_LIT)) {
679
693
  const lit = this.parseLiteral();
@@ -684,6 +698,15 @@ export class Parser {
684
698
  if (tok.type === TT.IDENT) {
685
699
  const name = this.consume(TT.IDENT).value;
686
700
 
701
+ // Check for !=
702
+ if (this.check(TT.OP_NEQ)) {
703
+ this.consume(TT.OP_NEQ);
704
+ console.log("DEBUG != right side token:", this.peek().value, "type:", TT[this.peek().type]);
705
+ const right = this.parseExpression();
706
+ if (!right) throw new SinthError("Expected expression after !=", this.peek().loc);
707
+ return { kind: "binary", left: { kind: "variable", name }, op: "!=", right };
708
+ }
709
+
687
710
 
688
711
  // Check for == (comparison)
689
712
  if (this.check(TT.OP_EQEQ)) {
@@ -693,13 +716,7 @@ export class Parser {
693
716
  return { kind: "binary", left: { kind: "variable", name }, op: "==", right };
694
717
  }
695
718
 
696
- // Check for !=
697
- if (this.check(TT.OP_NEQ)) {
698
- this.consume(TT.OP_NEQ);
699
- const right = this.parseExpression();
700
- if (!right) throw new SinthError("Expected expression after !=", this.peek().loc);
701
- return { kind: "binary", left: { kind: "variable", name }, op: "!=", right };
702
- }
719
+
703
720
 
704
721
  // Check for <
705
722
  if (this.check(TT.OP_LT)) {
@@ -1026,8 +1043,48 @@ private parseCompUse(): CompUse {
1026
1043
  }
1027
1044
  this.consume(TT.RBRACE);
1028
1045
  js += "}\n";
1046
+
1047
+ // Check for else
1048
+ if (this.check(TT.KW_ELSE)) {
1049
+ this.consume(TT.KW_ELSE);
1050
+ if (this.check(TT.LBRACE)) {
1051
+ this.consume(TT.LBRACE);
1052
+ js += "else {\n";
1053
+ while (!this.check(TT.RBRACE) && !this.check(TT.EOF)) {
1054
+ if (this.check(TT.IDENT)) {
1055
+ const name = this.consume(TT.IDENT).value;
1056
+ if (this.check(TT.EQUALS)) {
1057
+ this.consume(TT.EQUALS);
1058
+ const expr = this.parseExpression();
1059
+ if (expr) {
1060
+ js += " " + compileExprToJS({ kind: "assign", target: name, op: "=", right: expr }) + ";\n";
1061
+ }
1062
+ } else if (this.check(TT.OP_PLUS) && this.tokens[this.pos + 1]?.type === TT.EQUALS) {
1063
+ this.consume(TT.OP_PLUS);
1064
+ this.consume(TT.EQUALS);
1065
+ const expr = this.parseExpression();
1066
+ if (expr) {
1067
+ js += " " + compileExprToJS({ kind: "assign", target: name, op: "+=", right: expr }) + ";\n";
1068
+ }
1069
+ } else if (this.check(TT.OP_MINUS) && this.tokens[this.pos + 1]?.type === TT.EQUALS) {
1070
+ this.consume(TT.OP_MINUS);
1071
+ this.consume(TT.EQUALS);
1072
+ const expr = this.parseExpression();
1073
+ if (expr) {
1074
+ js += " " + compileExprToJS({ kind: "assign", target: name, op: "-=", right: expr }) + ";\n";
1075
+ }
1076
+ }
1077
+ } else {
1078
+ break;
1079
+ }
1080
+ }
1081
+ this.consume(TT.RBRACE);
1082
+ js += "}\n";
1083
+ }
1084
+ }
1085
+
1029
1086
  return js;
1030
- }
1087
+ }
1031
1088
 
1032
1089
  private parseForLoop(): ForLoop {
1033
1090
  const loc = this.consume(TT.KW_FOR).loc;
@@ -2382,7 +2439,7 @@ ${(bodyHTML.includes('data-sinth-if') || bodyHTML.includes('data-sinth-for')) ?
2382
2439
  }
2383
2440
  const cond = eval(t.dataset.sinthIf);
2384
2441
  // Find any previously inserted content
2385
- const existing = t.parentNode.querySelector('[data-sinth-if-anchor="' + t.dataset.sinthIf + '"]');
2442
+ const existing = t.parentNode.querySelector('[data-sinth-if-anchor="' + CSS.escape(t.dataset.sinthIf) + '"]');
2386
2443
  if (cond) {
2387
2444
  if (!existing) {
2388
2445
  // Insert new content with anchor marker
@@ -2413,7 +2470,7 @@ ${(bodyHTML.includes('data-sinth-if') || bodyHTML.includes('data-sinth-for')) ?
2413
2470
  if (!elseT.dataset.sinthOriginal) {
2414
2471
  elseT.dataset.sinthOriginal = elseT.innerHTML;
2415
2472
  }
2416
- const elseAnchor = t.parentNode.querySelector('[data-sinth-if-anchor="else-' + t.dataset.sinthIf + '"]');
2473
+ const elseAnchor = t.parentNode.querySelector('[data-sinth-if-anchor="' + CSS.escape('else-' + t.dataset.sinthIf) + '"]');
2417
2474
  const elseExisting = elseAnchor ? elseAnchor.nextSibling : null;
2418
2475
  if (!elseExisting || elseExisting === t) {
2419
2476
  const anchor = document.createElement('span');