Skip to content

Commit

Permalink
增加x:pre-parse支持
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Nov 23, 2024
1 parent 8d2bd9c commit 7d53687
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 6 deletions.
2 changes: 2 additions & 0 deletions nop-xdefs/src/main/resources/_vfs/nop/schema/xdef.xdef
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ xdef自身的元模型定义。通过此文件实现对xdef的自举定义,即

</meta:define>

<xdef:pre-parse meta:value="xpl"/>

<xdef:post-parse meta:value="xpl"/>

</meta:unknown-tag>
2 changes: 2 additions & 0 deletions nop-xlang/src/main/java/io/nop/xlang/xdef/IXDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ default IXDefNode getSubComponent(String name) {

String getXdefDefaultExtends();

IEvalAction getXdefPreParse();

IEvalAction getXdefPostParse();

/**
Expand Down
4 changes: 3 additions & 1 deletion nop-xlang/src/main/java/io/nop/xlang/xdef/XDefKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public static XDefKeys of(XNode node) {
public final Set<String> ROOT_CHILD_NAMES;

public final String POST_PARSE;
public final String PRE_PARSE;

public XDefKeys(String ns) {
NS = ns;
Expand Down Expand Up @@ -158,6 +159,7 @@ public XDefKeys(String ns) {
UNIT = getFullName(ns, "unit");

POST_PARSE = getFullName(ns, "post-parse");
PRE_PARSE = getFullName(ns, "pre-parse");

ATTR_NAMES = CollectionHelper.buildImmutableSet(SUPPORT_EXTENDS, BEAN_CLASS, BEAN_INTERFACES, BEAN_TAG_PROP,
BEAN_BODY_PROP, BEAN_COMMENT_PROP, BEAN_CHILD_NAME, BEAN_PROP, ID, REF_RESOLVED, BEAN_IMPLEMENTS_TYPES,
Expand All @@ -175,7 +177,7 @@ public XDefKeys(String ns) {
CHILD_NAMES = CollectionHelper.buildImmutableSet(DEFINE, UNIT, UNKNOWN_TAG, GETTER, SETTER, IMPORT_EXPR,
EXPORT_EXPR, PROP);
ROOT_CHILD_NAMES = CollectionHelper.buildImmutableSet(DEFINE, UNKNOWN_TAG, GETTER, SETTER, IMPORT_EXPR,
EXPORT_EXPR, POST_PARSE, PROP);
EXPORT_EXPR, POST_PARSE, PRE_PARSE, PROP);
}

private static String getFullName(String ns, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ public abstract class _XDefinition extends io.nop.xlang.xdef.impl.XDefNode {
*/
private io.nop.core.lang.eval.IEvalAction _xdefPostParse ;

/**
*
* xml name: xdef:pre-parse
*
*/
private io.nop.core.lang.eval.IEvalAction _xdefPreParse ;

/**
*
* xml name: xdef:prop-ns
Expand Down Expand Up @@ -311,6 +318,25 @@ public void setXdefPostParse(io.nop.core.lang.eval.IEvalAction value){
}


/**
*
* xml name: xdef:pre-parse
*
*/

public io.nop.core.lang.eval.IEvalAction getXdefPreParse(){
return _xdefPreParse;
}


public void setXdefPreParse(io.nop.core.lang.eval.IEvalAction value){
checkAllowChange();

this._xdefPreParse = value;

}


/**
*
* xml name: xdef:prop-ns
Expand Down Expand Up @@ -414,6 +440,7 @@ protected void outputJson(IJsonHandler out){
out.putNotNull("xdefParseKeepComment",this.getXdefParseKeepComment());
out.putNotNull("xdefParserClass",this.getXdefParserClass());
out.putNotNull("xdefPostParse",this.getXdefPostParse());
out.putNotNull("xdefPreParse",this.getXdefPreParse());
out.putNotNull("xdefPropNs",this.getXdefPropNs());
out.putNotNull("xdefTransform",this.getXdefTransform());
out.putNotNull("xdefTransformerClass",this.getXdefTransformerClass());
Expand All @@ -439,6 +466,7 @@ protected void copyTo(XDefinition instance){
instance.setXdefParseKeepComment(this.getXdefParseKeepComment());
instance.setXdefParserClass(this.getXdefParserClass());
instance.setXdefPostParse(this.getXdefPostParse());
instance.setXdefPreParse(this.getXdefPreParse());
instance.setXdefPropNs(this.getXdefPropNs());
instance.setXdefTransform(this.getXdefTransform());
instance.setXdefTransformerClass(this.getXdefTransformerClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ protected XDefinition doParseNode(XNode node) {
def.setXdefModelNameProp(xdefModelNameProp);
def.setXdefModelVersionProp(xdefModelVersionProp);

XNode preParse = node.uniqueChild(keys.PRE_PARSE);
if (preParse != null) {
def.setXdefPreParse(getCompileTool().compileTagBody(preParse));
}

XNode postParse = node.uniqueChild(keys.POST_PARSE);
if (postParse != null) {
def.setXdefPostParse(getCompileTool().compileTagBody(postParse));
Expand Down
39 changes: 36 additions & 3 deletions nop-xlang/src/main/java/io/nop/xlang/xdsl/AbstractDslParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@
import io.nop.core.lang.eval.IEvalAction;
import io.nop.core.lang.eval.IEvalScope;
import io.nop.core.lang.xml.XNode;
import io.nop.core.reflect.ReflectionManager;
import io.nop.core.reflect.impl.DefaultClassResolver;
import io.nop.core.resource.IResource;
import io.nop.core.resource.component.ResourceComponentManager;
import io.nop.core.resource.component.parse.AbstractResourceParser;
import io.nop.core.type.IGenericType;
import io.nop.core.type.IRawTypeResolver;
import io.nop.xlang.XLangConstants;
import io.nop.xlang.api.IXLangCompileScope;
import io.nop.xlang.api.XLang;
import io.nop.xlang.api.XLangCompileTool;
import io.nop.xlang.ast.ImportAsDeclaration;
import io.nop.xlang.ast.definition.ScopeVarDefinition;
import io.nop.xlang.xdef.IXDefinition;
import io.nop.xlang.xpl.tags.ImportTagCompiler;
import org.slf4j.Logger;
Expand Down Expand Up @@ -114,12 +118,39 @@ protected T doParseResource(IResource resource) {
setXdef(extendResult.getXdef());

applyCompileConfig(extendResult.getConfig());
runPreParse(extendResult);

T parseResult = doParseNode(extendResult.getNode());

parseResult = runPostParse(parseResult, extendResult);
return parseResult;
}

protected void runPreParse(XDslExtendResult extendResult) {
XNode rootNode = extendResult.getNode();

IXLangCompileScope scope = compileTool.getScope();
scope.setLocalValue(null, XLangConstants.SCOPE_VAR_DSL_ROOT, rootNode);

if (getXdef().getXdefPreParse() != null) {
getXdef().getXdefPreParse().invoke(scope);
}

if (extendResult.getPreParse() != null) {
IEvalAction preParse = compileTool.compileTagBody(extendResult.getPreParse());
if (preParse != null) {
preParse.invoke(scope);
}
}

for (String name : scope.keySet()) {
Object value = scope.getLocalValue(name);
IGenericType type = value == null ? null : ReflectionManager.instance().buildRawType(value.getClass());
scope.registerScopeVarDefinition(ScopeVarDefinition.readOnly(name, type), true);
}
}


protected void applyCompileConfig(XNode config) {
if (config == null)
return;
Expand All @@ -146,17 +177,17 @@ protected void applyCompileConfig(XNode config) {
protected T runPostParse(T parseResult, XDslExtendResult extendResult) {

if (getXdef().getXdefPostParse() != null) {
IEvalScope scope = XLang.newEvalScope();
IEvalScope scope = compileTool.getScope();
scope.setLocalValue(null, XLangConstants.SYS_VAR_DSL_MODEL, parseResult);
Object ret = getXdef().getXdefPostParse().invoke(scope);
if (ret != null)
parseResult = (T) ret;
}

if (extendResult.getPostParse() != null) {
IEvalAction postParse = compileTool.compileTagBody(extendResult.getPostExtends());
IEvalAction postParse = compileTool.compileTagBody(extendResult.getPostParse());
if (postParse != null) {
IEvalScope scope = XLang.newEvalScope();
IEvalScope scope = compileTool.getScope();
scope.setLocalValue(null, XLangConstants.SYS_VAR_DSL_MODEL, parseResult);
Object ret = postParse.invoke(scope);
if (ret != null)
Expand Down Expand Up @@ -197,6 +228,8 @@ protected T parseFromNode0(XNode node) {
compileTool = XLang.newCompileTool();

applyCompileConfig(extendResult.getConfig());
runPreParse(extendResult);

T parseResult = doParseNode(extendResult.getNode());

parseResult = runPostParse(parseResult, extendResult);
Expand Down
12 changes: 12 additions & 0 deletions nop-xlang/src/main/java/io/nop/xlang/xdsl/XDslExtendResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class XDslExtendResult {
private XNode node;
private XNode config;
private XNode postExtends;
private XNode preParse;
private XNode postParse;
private boolean validated;

Expand Down Expand Up @@ -70,6 +71,9 @@ public XNode getNodeForDump() {
ret.prependChild(cfg);
}

if (preParse != null)
ret.appendChild(preParse.cloneInstance());

if (postParse != null) {
ret.appendChild(postParse.cloneInstance());
}
Expand All @@ -78,6 +82,14 @@ public XNode getNodeForDump() {
return ret;
}

public XNode getPreParse() {
return preParse;
}

public void setPreParse(XNode preParse) {
this.preParse = preParse;
}

public XNode getBase() {
return base;
}
Expand Down
8 changes: 7 additions & 1 deletion nop-xlang/src/main/java/io/nop/xlang/xdsl/XDslExtender.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public XDslExtendResult xtend(IXDefinition def, IXDefNode defNode, XNode node, X
}

List<IXNodeTransformer> loadTransformers(IXDefinition xdef, IEvalScope scope) {
if(xdef == null)
if (xdef == null)
return null;

if (transformers != null)
Expand Down Expand Up @@ -160,6 +160,7 @@ void normalize(XDslExtendResult result) {
XNode node = result.getNode();
XNode config = node.uniqueChild(keys.CONFIG);
XNode postExtends = node.uniqueChild(keys.POST_EXTENDS);
XNode preParse = node.uniqueChild(keys.PRE_PARSE);
XNode postParse = node.uniqueChild(keys.POST_PARSE);

if (config != null) {
Expand All @@ -170,6 +171,10 @@ void normalize(XDslExtendResult result) {
postExtends.detach();
}

if (preParse != null) {
preParse.detach();
}

if (postParse != null) {
postParse.detach();
}
Expand All @@ -178,6 +183,7 @@ void normalize(XDslExtendResult result) {

result.setConfig(config);
result.setPostExtends(postExtends);
result.setPreParse(preParse);
result.setPostParse(postParse);
}

Expand Down
5 changes: 4 additions & 1 deletion nop-xlang/src/main/java/io/nop/xlang/xdsl/XDslKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public final class XDslKeys implements Serializable {
public final String CONFIG;
public final String GEN_EXTENDS;
public final String POST_EXTENDS;
public final String PRE_PARSE;
public final String POST_PARSE;

public final String PROTOTYPE;
Expand Down Expand Up @@ -67,6 +68,7 @@ public XDslKeys(String ns) {
this.CONFIG = getFullName(ns, "config");
this.GEN_EXTENDS = getFullName(ns, "gen-extends");
this.POST_EXTENDS = getFullName(ns, "post-extends");
this.PRE_PARSE = getFullName(ns, "pre-parse");
this.POST_PARSE = getFullName(ns, "post-parse");
this.PROTOTYPE = getFullName(ns, "prototype");
this.PROTOTYPE_SUPER = getFullName(ns, "prototype-super");
Expand All @@ -86,7 +88,8 @@ public XDslKeys(String ns) {
this.ATTR_NAMES = CollectionHelper.buildImmutableSet(NAME, EXTENDS, PROTOTYPE, PROTOTYPE_OVERRIDE, ABSTRACT,
SCHEMA, KEY_ATTR, VALIDATED, VIRTUAL, INHERIT, FINAL, OVERRIDE, DUMP);

this.CHILD_NAMES = CollectionHelper.buildImmutableSet(ARGS, GEN_EXTENDS, CONFIG, POST_EXTENDS, POST_PARSE,
this.CHILD_NAMES = CollectionHelper.buildImmutableSet(ARGS, GEN_EXTENDS, CONFIG, POST_EXTENDS,
PRE_PARSE, POST_PARSE,
SUPER, PROTOTYPE_SUPER);
}

Expand Down
33 changes: 33 additions & 0 deletions nop-xlang/src/test/java/io/nop/xlang/xdsl/TestMacroGen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.nop.xlang.xdsl;

import io.nop.core.initialize.CoreInitialization;
import io.nop.core.lang.xml.IXNodeGenerator;
import io.nop.core.lang.xml.XNode;
import io.nop.core.model.object.DynamicObject;
import io.nop.core.unittest.BaseTestCase;
import io.nop.xlang.api.XLang;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestMacroGen extends BaseTestCase {
@BeforeAll
public static void init() {
CoreInitialization.initialize();
}

@AfterAll
public static void destroy() {
CoreInitialization.destroy();
}

@Test
public void testGenExtends() {
DynamicObject obj = (DynamicObject) new DslModelParser().parseFromVirtualPath("/test/macro/test-macro-gen.xml");
IXNodeGenerator generator = (IXNodeGenerator) obj.prop_get("filter");
XNode node = generator.generateNode(XLang.newEvalScope());
assertEquals(1, node.child(0).attrInt("value"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<model x:schema="/nop/schema/xdef.xdef" xmlns:xdef="/nop/schema/xdef.xdef"
xmlns:x="/nop/schema/xdsl.xdef">

<filter xdef:value="xpl-node"/>

</model>
26 changes: 26 additions & 0 deletions nop-xlang/src/test/resources/_vfs/test/macro/test-macro-gen.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<model x:schema="/test/macro/test-macro-gen.xdef" xmlns:x="/nop/schema/xdsl.xdef" xmlns:batch="batch">
<x:pre-parse>
<c:script>
const task = _dsl_root.childByTag('batch:task');
_dsl_root.removeChild(task);

const count = task.childByTag('steps').getChildCount();

assign('metaChildCount', count);
</c:script>
</x:pre-parse>

<batch:task>
<steps>
<xpl id="step1">
<source>
<c:log info="ss"/>
</source>
</xpl>
</steps>
</batch:task>

<filter>
<eq name="count" value="#{metaChildCount}" />
</filter>
</model>

0 comments on commit 7d53687

Please sign in to comment.