[stringtemplate-interest] Maps

Terence Parr parrt at cs.usfca.edu
Thu Aug 20 11:52:33 PDT 2009


  can you tell me why you  need to play around with the implementation  
inside?
Ter
On Aug 20, 2009, at 12:27 AM, Jacques Victor wrote:

> Hi there,
>
> I've had a look at that function on the STG but my problem exists  
> when iterating through the ST's chunks. Each chunk object has no  
> clear 'Name' method from what I can see therefore I cannot pass  
> getMap() the name of the map.
>
> I see you mentioned a 'Map' object on the getMap() STG return type.  
> The version of assembly I'm using does not seem to have this object.
>
> Below is a sample of my code to give more context around the question:
>
>
> Assemblies:
> antlr.runtime.dll - V 2.7.7.3
> StringTemplate.dll - V 3.0.1.6846
>
> Expected Output:
> My Dog barks.
>
>
> C# code snippet:
>
> Public void Process()
> {
>     Console.WriteLine(RenderTemplate(new Dictionary<string, object>() 
> {"AnimalName","Dog"}));
> }
>
> public string RenderTemplate(Dictionary<string, object> parameters)
> {
>     StringTemplateGroup group = new StringTemplateGroup("mygroup",  
> "c:/temp/my.stg");
>
>     StringTemplate st = group.GetInstanceOf("test1");
>
>     foreach (object chunk in st.Chunks)
>     {
>        if (chunk != null)
>        {
>           if (chunk.GetType() == typeof(ASTExpr))
>           {
>             //
>             // Insert code here to determine if chunk is a Map
>             //
>             string mapName = ((ASTExpr)chunk).??????????
>             /*
>                 1. chunk.ToString returns "( . soundNameMap ( value  
> AnimalName ) )"....Based on the sample Template below.
>                     I don't want to implement a dirty fix here to  
> strip out the map name
>
>                 2. (((StringTemplateAST)((ASTExpr) 
> (chunk)).AST).getText() returns "."
>
>                 3. (((StringTemplateAST)((ASTExpr)(chunk)).AST).Type  
> returns int 24. I'm unable to determine what this value maps to?
>                     I've searched on the StringTemplate  
> documentation and can find no enumeration mapping.
>             */
>
>             //I do not have a 'Map' object in the assembly version  
> listed above.
>             IDictionary map = group.GetMap(mapName);
>
>             //Don't do anything if the chunk is a map.
>             if(map == null))
>             {
>                 // No map found thus, for this example, we assume  
> it's a placeholder variable in the template
>                 if(parameters.ContainsKey(placeholder)
>                 {
>                     st.SetAttribute(chunk.ToString(),  
> parameters[placeholder]);
>                 }
>                 else
>                 {
>                     throw new ParameterNotFoundException("Parameter  
> '" + chunk.ToString() + "' is not in Dictionary.");
>                 }
>             }
>           }
>         }
>     }
>
>     return st.ToString();
> }
>
> STG Template - "my.stg"
>
> group mygroup;
>
> soundNameMap ::= [
>    "Dog":      "barks",
>    "Cat": "meows",
>     default:   "N/A"
> ]
>
> test1(AnimalName) ::= <<
> My $AnimalName$ $soundNameMap.(AnimalName)$.
> >>
>
>
>
>
>
> 2009/8/19 Terence Parr <parrt at cs.usfca.edu>
> see
>
>        public Map getMap(String name) {}
>
> in STG. or subclass and walk field maps.
> T
>
>
> On Aug 18, 2009, at 6:38 AM, Jacques Victor wrote:
>
> Hi there,
>
> I've recently started using StringTemplate and today have run into a  
> brick wall...so to speak...
>
> I'm loading my *stg file in .Net (C#) and setting the stringtemplate  
> placeholder variables via a Dictionary. Once determining a chunk is  
> of type 'ASTExpr', I I'm struggling to determine if that chunk is  
> indeed a map and therefore not checking against my Dictionary and  
> throwing an error if I don't find the chunk placeholder variable in  
> the Dictionary.
>
> Is it possible to get all the maps defined in a Stringtemplategroup  
> file or alternatively as I iterate through the chunks, in the  
> StringTemplate object, be able to identify a chunk as a map?
>
> I've searched the internet but can't find any enum/type that would  
> make a map chunk distinguishable, from other chunks, in .Net.
>
> Any help would greatly be appreciated.
>
> Thanks in advance.
>
> Regards
> Jacques
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>
>



More information about the stringtemplate-interest mailing list