raw haxe block syntax. close #54

This commit is contained in:
2022-06-05 16:50:12 +00:00
parent c3c153d2d3
commit bf540c3784
5 changed files with 37 additions and 7 deletions

View File

@@ -438,6 +438,12 @@ class Kiss {
} catch (err:Exception) {
throw KissError.fromExp(exp, 'Haxe parse error: $err');
};
case RawHaxeBlock(code):
try {
Context.parse('{$code}', exp.macroPos());
} catch (err:Exception) {
throw KissError.fromExp(exp, 'Haxe parse error: $err');
};
case FieldExp(field, innerExp):
EField(convert(innerExp), field).withMacroPosOf(exp);
case KeyValueExp(keyExp, valueExp):

View File

@@ -68,7 +68,17 @@ class Reader {
CallExp(Symbol("quote").withPos(stream.position()), [assertRead(stream, k)]);
};
readTable["#|"] = (stream:Stream, k) -> RawHaxe(stream.expect("closing |#", () -> stream.takeUntilAndDrop("|#")));
readTable["#|"] = (stream:Stream, k) -> {
var pos = stream.position();
var haxe = stream.expect("closing |#", () -> stream.takeUntilAndDrop("|#"));
var def = RawHaxe(haxe);
KissError.warnFromExp(def.withPos(pos), '#|rawHaxe()|# expressions are deprecated because they only parse one statement and ignore the rest. Try this: #{$haxe}#');
def;
};
readTable["#{"] = (stream:Stream, k) -> {
RawHaxeBlock(stream.expect("closing }#", () -> stream.takeUntilAndDrop("}#")));
};
readTable[":"] = (stream:Stream, k) -> TypedExp(nextToken(stream, "a type path"), assertRead(stream, k));
@@ -495,8 +505,11 @@ class Reader {
case RawHaxe(code):
// #| haxeCode() |#
'#| $code |#';
case RawHaxeBlock(code):
// #{ haxeCode(); moreHaxeCode(); }#
'#{ $code }#';
case TypedExp(path, exp):
// :type [exp]
// :Type [exp]
':$path ${exp.def.toString()}';
case MetaExp(meta, exp):
// &meta

View File

@@ -12,7 +12,8 @@ enum ReaderExpDef {
ListExp(exps:Array<ReaderExp>); // [v1 v2 v3]
StrExp(s:String); // "literal"
Symbol(name:String); // s
RawHaxe(code:String); // #| haxeCode() |#
RawHaxe(code:String); // #| haxeCode() |# // deprecated!
RawHaxeBlock(code:String); // #{ haxeCode(); moreHaxeCode(); }#
TypedExp(path:String, exp:ReaderExp); // :Type [exp]
MetaExp(meta:String, exp:ReaderExp); // &meta [exp]
FieldExp(field:String, exp:ReaderExp); // .field [exp]

View File

@@ -16,7 +16,7 @@ class BasicTestCase extends Test {
}
function testHaxeInsertion() {
Assert.equals(23, BasicTestCase.mathResult);
_testHaxeInsertion();
}
function testStaticFunction() {

View File

@@ -9,8 +9,18 @@
// (var) declares static variables
(var message "Howdy")
// #| ... |# parses and injects raw Haxe code
(var mathResult #|5 + 6 * 3|#) // Order of operations will apply
(function _testHaxeInsertion []
// #| ... |# parses and injects raw Haxe code.
// Order of operations will apply
(Assert.equals 23 #|5 + 6 * 3|#)
// #{ ... }# parses and injects a raw Haxe block. It is preferred over #| |#
(let [&mut a 5 &mut b 6]
#{
a += 6; b += 5;
}#
(Assert.equals 11 a)
(Assert.equals 11 b)))
// (function) declares static functions
(function myFloor [num]
@@ -327,7 +337,7 @@
(try
(assert false (+ "false " "should " "have " "been " "true"))
(catch [:String message]
(Assert.equals "kiss/src/test/cases/BasicTestCase.kiss:328:13: Assertion failed: false should have been true
(Assert.equals "kiss/src/test/cases/BasicTestCase.kiss:338:13: Assertion failed: false should have been true
From:[(assert false (+ \"false \" \"should \" \"have \" \"been \" \"true\"))]" message)))
(assert true)