Permalink
Browse files

Added support of default namespace for Nemerle.XML macros

  • Loading branch information...
1 parent d6f8079 commit 4495e924eeaff52b6d295d2db7f1d47d6f06e368 @ka3a4ok committed Oct 17, 2011
@@ -41,7 +41,7 @@ namespace Nemerle.Xml
//timer.Reset();
//timer.Start();
def transformer = XmlAstToXLinq(typer, parser);
- def res = transformer.Transform(result, Set());
+ def res = transformer.Transform(result, Set(), "");
//timer.Stop();
//Message.Hint($"Transform tooke: $(timer.Elapsed)");
res
@@ -38,13 +38,17 @@ namespace Nemerle.Xml
pExpr
}
- SplicablePExprName(value : X.Splicable, nsVars : Set[string]) : PExpr
+ SplicablePExprName(value : X.Splicable, nsVars : Set[string], defaultns:string="") : PExpr
{
Util.locate(ToLocation(value),
match (value)
{
| Splicable.PrefiedName(ns, name) => DefineXmlnsImpl.MakeXNameFieldRef(_typer, ToLocation(value), ns, name, nsVars)
- | Splicable.Value(value) => <[ L.XName.Get($(value : string)) ]>
+ | Splicable.Value(value) =>
+ if(defaultns.IsEmpty())
+ <[ L.XName.Get($(value : string)) ]>
+ else
+ <[ L.XNamespace.Get($(defaultns : string)).GetName($(value : string))]>
| Splicable.Expr(expr) => Parse(ToLocation(value), expr)
| Splicable.ListExpr => Message.FatalError(<#The "..$" not supportend in name context.#>);
| Splicable.ControlExpr => assert(false)
@@ -63,17 +67,17 @@ namespace Nemerle.Xml
| Splicable.ControlExpr => assert(false)
})
}
- public Transform(xmlAst : XmlAst, nsVars : Set[string]) : PExpr
+ public Transform(xmlAst : XmlAst, nsVars : Set[string], defaultns:string) : PExpr
@VladD2

VladD2 Oct 17, 2011

По нашим правилам форматирования между любыми операторными символами и идентификаторами должны быть пробелы.

{
- Util.locate(ToLocation(xmlAst), TransformImpl(xmlAst, nsVars))
+ Util.locate(ToLocation(xmlAst), TransformImpl(xmlAst, nsVars, defaultns))
}
- public TransformImpl(xmlAst : XmlAst, nsVars : Set[string]) : PExpr
+ public TransformImpl(xmlAst : XmlAst, nsVars : Set[string], defaultns:string) : PExpr
{
match (xmlAst)
{
| Attr(name, Splicable.Expr as value) =>
- def name1 = SplicablePExprName(name, nsVars);
+ def name1 = SplicablePExprName(name, nsVars, defaultns);
def value1 = SplicablePExprStr(value);
<[ { // If value of expression is null we return null which cause ignorin the attribute
@@ -82,7 +86,7 @@ namespace Nemerle.Xml
} ]>
| Attr(name, value) =>
- def name1 = SplicablePExprName(name, nsVars);
+ def name1 = SplicablePExprName(name, nsVars, defaultns);
def value1 = SplicablePExprStr(value);
<[ L.XAttribute($name1, $value1) ]>
@@ -91,7 +95,7 @@ namespace Nemerle.Xml
| Text(_) => assert(false);
| Tag(name, XmlAst.AttrSplice(expr, SpliceType.Foreach) as c :: content) =>
def tag = XmlAst.Tag(xmlAst.StartPos, xmlAst.EndPos, name, content);
- def tagExpr = TransformImpl(tag, nsVars);
+ def tagExpr = TransformImpl(tag, nsVars, defaultns);
def header = Parse(ToLocation(c), expr);
<[ {
def lst = SCG.List();
@@ -111,13 +115,14 @@ namespace Nemerle.Xml
| Tag(name, XmlAst.AttrSplice(expr, SpliceType.Unless) as c :: content) with isWhen = false
=>
def tag = XmlAst.Tag(xmlAst.StartPos, xmlAst.EndPos, name, content);
- def tagExpr = TransformImpl(tag, nsVars);
+ def tagExpr = TransformImpl(tag, nsVars, defaultns);
def cond1 = Parse(ToLocation(c), expr);
def cond2 = if (isWhen) cond1 else <[ ! $cond1 ]>;
<[ if ($cond2) $tagExpr else null ]>
| Tag(name, content) =>
mutable nsVars2 = nsVars;
+ mutable defns2=defaultns;
def (nsAttrDef, cont, xmlNsVars) = content.Fold(([], [], []), (x, (nsAttrDef, cont, nsVarsDef)) =>
match (x)
{
@@ -134,12 +139,18 @@ namespace Nemerle.Xml
(nsAttrDef, cont, nsVarsDef)
| XmlAst.AttrSplice(_expr, SpliceType.When) => (nsAttrDef, x :: cont, nsVarsDef)
+ | XmlAst.Attr(Splicable.Value("xmlns")as attr, Splicable.Value(val)) =>
+ defns2=val;
+ Util.locate(ToLocation(attr),
+ {
+ (nsAttrDef, cont, nsVarsDef)
+ });
| _ => (nsAttrDef, x :: cont, nsVarsDef)
});
- def nameExpr1 = SplicablePExprName(name, nsVars2);
+ def nameExpr1 = SplicablePExprName(name, nsVars2, defns2);
def nameExpr2 = Util.locate(ToLocation(name), <[ $nameExpr1 : L.XName ]>);
- def cont = nsAttrDef + cont.RevMap(x => <[ $(Transform(x, nsVars2)) : object ]>);
+ def cont = nsAttrDef + cont.RevMap(x => <[ $(Transform(x, nsVars2, defns2)) : object ]>);
def expr = xmlNsVars + [<[ L.XElement($nameExpr2, array[..$cont]); ]>];
<[ { ..$expr } ]>
@@ -63,8 +63,14 @@ module Program
def res2 = xml <# <ns2:Папа xmlns:ns2="namespace-2"><$name $when (z.HasValue) ns1:a="123"/></> #>;
WriteLine(res2);
- def ƱƲƳ = 123;
- WriteLine(ƱƲƳ);
- //_ = ReadLine();
+ def res=
+ xml
+ <# <a xmlns="ns1">
+ <b/>
+ <c xmlns="ns2"/>
+ <n:d xmlns:n="ns3"/>
+
+ </a>
+ #>
}
}

0 comments on commit 4495e92

Please sign in to comment.