private object EvaluateInPlace(string content, IInternalContextAdapter context) { try { SimpleNode inlineNode = runtimeServices.Parse(new StringReader(content), context.CurrentTemplateName, false); inlineNode.Init(context, runtimeServices); return(Evaluate(inlineNode, context)); } catch (System.Exception) { throw new ArgumentException(string.Format("Problem evaluating dictionary entry with content {0}", content)); } }
/* Override this method if you want to customize how the node dumps * out its children. */ public void Dump(String prefix) { Console.Out.WriteLine(ToString(prefix)); if (children != null) { for (int i = 0; i < children.Length; ++i) { SimpleNode n = (SimpleNode)children[i]; if (n != null) { n.Dump(string.Format("{0} ", prefix)); } } } }
/// <summary> /// Evaluate the node. /// </summary> public override Object Value(IInternalContextAdapter context) { int size = ChildrenCount; IDictionary objectMap = new Hashtable(); for (int i = 0; i < size; i += 2) { SimpleNode keyNode = (SimpleNode)GetChild(i); SimpleNode valueNode = (SimpleNode)GetChild(i + 1); Object key = (keyNode == null ? null : keyNode.Value(context)); Object value = (valueNode == null ? null : valueNode.Value(context)); objectMap.Add(key, value); } return(objectMap); }
/* * CODE FOR ALTERNATE IMPL : please ignore. I will remove when comfortable with current. */ /// <summary> not used in current impl /// * /// Constructor for alternate impl where VelProxy class would make new /// VMProxyArg objects, and use this constructor to avoid re-parsing the /// reference args /// * /// that impl also had the VMProxyArg carry it's context /// </summary> public VMProxyArg(VMProxyArg model, IInternalContextAdapter c) { userContext = c; contextReference = model.ContextReference; callerReference = model.CallerReference; nodeTree = model.NodeTree; staticObject = model.StaticObject; type = model.Type; if (nodeTree != null) { numTreeChildren = nodeTree.ChildrenCount; } if (type == ParserTreeConstants.REFERENCE) { if (numTreeChildren == 0) { /* * use the reference node to do this... */ singleLevelRef = ((ASTReference) nodeTree).RootString; } } }
/// <summary> does the housekeeping upon creating. If a dynamic type /// it needs to make an AST for further get()/set() operations /// Anything else is constant. /// </summary> private void setup() { switch(type) { case ParserTreeConstants.INTEGER_RANGE: case ParserTreeConstants.REFERENCE: case ParserTreeConstants.OBJECT_ARRAY: case ParserTreeConstants.STRING_LITERAL: case ParserTreeConstants.TEXT: { /* * dynamic types, just render */ constant = false; try { /* * fakie : wrap in directive to get the parser to treat our args as args * it doesn't matter that #include() can't take all these types, because we * just want the parser to consider our arg as a Directive/VM arg rather than * as if inline in schmoo */ String buff = string.Format("#include({0} ) ", callerReference); //ByteArrayInputStream inStream = new ByteArrayInputStream( buff.getBytes() ); //UPGRADE_ISSUE: The equivalent of constructor 'java.io.BufferedReader.BufferedReader' is incompatible with the expected type in C#. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1109"' TextReader br = new StringReader(buff); nodeTree = runtimeServices.Parse(br, string.Format("VMProxyArg:{0}", callerReference), true); /* * now, our tree really is the first DirectiveArg(), and only one */ nodeTree = (SimpleNode) nodeTree.GetChild(0).GetChild(0); /* * sanity check */ if (nodeTree != null && nodeTree.Type != type) { runtimeServices.Error("VMProxyArg.setup() : programmer error : type doesn't match node type."); } /* * init. We can do this as they are only references */ nodeTree.Init(null, runtimeServices); } catch(System.Exception e) { runtimeServices.Error(string.Format("VMProxyArg.setup() : exception {0} : {1}", callerReference, e)); } break; } case ParserTreeConstants.TRUE: { constant = true; staticObject = true; break; } case ParserTreeConstants.FALSE: { constant = true; staticObject = false; break; } case ParserTreeConstants.NUMBER_LITERAL: { constant = true; staticObject = Int32.Parse(callerReference); break; } case ParserTreeConstants.WORD: { /* * this is technically an error... */ runtimeServices.Error( string.Format( "Unsupported arg type : {0} You most likely intended to call a VM with a string literal, so enclose with ' or \" characters. (VMProxyArg.setup())", callerReference)); constant = true; staticObject = new String(callerReference.ToCharArray()); break; } default: { runtimeServices.Error(string.Format(" VMProxyArg.setup() : unsupported type : {0}", callerReference)); } break; } }
internal void parseTree(IInternalContextAdapter internalContextAdapter) { try { //UPGRADE_ISSUE: The equivalent of constructor 'java.io.BufferedReader.BufferedReader' is incompatible with the expected type in C#. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1109"' TextReader br = new StringReader(macroBody); nodeTree = Enclosing_Instance.runtimeServices.Parse(br, string.Format("VM:{0}", macroName), true); nodeTree.Init(internalContextAdapter, null); } catch(System.Exception e) { Enclosing_Instance.runtimeServices.Error( string.Format("VelocimacroManager.parseTree() : exception {0} : {1}", macroName, e)); } }
/// <summary> init : we don't have to do much. Init the tree (there /// shouldn't be one) and then see if interpolation is turned on. /// </summary> public override Object Init(IInternalContextAdapter context, Object data) { base.Init(context, data); /* * the stringlit is set at template parse time, so we can * do this here for now. if things change and we can somehow * create stringlits at runtime, this must * move to the runtime execution path * * so, only if interpolation is turned on AND it starts * with a " AND it has a directive or reference, then we * can interpolate. Otherwise, don't bother. */ interpolate = FirstToken.Image.StartsWith("\"") && ((FirstToken.Image.IndexOf('$') != - 1) || (FirstToken.Image.IndexOf('#') != - 1)); /* * get the contents of the string, minus the '/" at each end */ image = FirstToken.Image.Substring(1, (FirstToken.Image.Length - 1) - (1)); /* * tack a space on the end (dreaded <MORE> kludge) */ interpolateImage = string.Format("{0} ", image); if (interpolate) { /* * now parse and init the nodeTree */ TextReader br = new StringReader(interpolateImage); /* * it's possible to not have an initialization context - or we don't * want to trust the caller - so have a fallback value if so * * Also, do *not* dump the VM namespace for this template */ nodeTree = runtimeServices.Parse(br, (context != null) ? context.CurrentTemplateName : "StringLiteral", false); /* * init with context. It won't modify anything */ nodeTree.Init(context, runtimeServices); } return data; }
private object Evaluate(SimpleNode inlineNode, IInternalContextAdapter context) { if (inlineNode.ChildrenCount == 1) { INode child = inlineNode.GetChild(0); return child.Value(context); } else { StringBuilder result = new StringBuilder(); for(int i = 0; i < inlineNode.ChildrenCount; i++) { INode child = inlineNode.GetChild(i); if (child.Type == ParserTreeConstants.REFERENCE) { result.Append(child.Value(context)); } else { result.Append(child.Literal); } } return result.ToString(); } }
public virtual Object Visit(SimpleNode node, Object data) { data = node.ChildrenAccept(this, data); return data; }
/// <summary> /// parses the macro. We need to do this here, at init time, or else /// the local-scope template feature is hard to get to work :) /// </summary> private void parseTree(String[] callArgs) { try { TextReader br = new StringReader(macroBody); // now parse the macro - and don't dump the namespace nodeTree = runtimeServices.Parse(br, ns, false); // now, to make null references render as proper schmoo // we need to tweak the tree and change the literal of // the appropriate references // we only do this at init time, so it's the overhead // is irrelevant Hashtable hm = new Hashtable(); for(int i = 1; i < argArray.Length; i++) { String arg = callArgs[i - 1]; // if the calling arg is indeed a reference // then we add to the map. We ignore other // stuff if (arg[0] == '$') { hm[argArray[i]] = arg; } } // now make one of our reference-munging visitor, and // let 'er rip VMReferenceMungeVisitor v = new VMReferenceMungeVisitor(hm); nodeTree.Accept(v, null); } catch(System.Exception e) { runtimeServices.Error(string.Format("VelocimacroManager.parseTree() : exception {0} : {1}", macroName, e)); } }
/// <summary>Display a SimpleNode /// </summary> public override Object Visit(SimpleNode node, Object data) { return ShowNode(node, data); }