.NET Framework Bookmark and Share   
 index > Microsoft Codename 'Oslo' > How can one preserve comments in MGrammar?
 

How can one preserve comments in MGrammar?

without sacrificing interleave, of course.

-Ciper
Ceyhun Ciper
Thanks. I'm working on an answer for you with the "M" team--there is a way to tap into the interleave stream during parsing and get the source location and the comments. Seems like what you describe for Antlr. There are just some details I'm clarifying for you still.

.Kraig
  • Marked As Answer byCeyhun Ciper Thursday, September 17, 2009 5:12 AM
  •  
Kraig Brockschmidt
Yes, Intellipad doesn't show comments in the output graph. But yes, the API does support this through the ISourceInfo interface. For example, I added this piece to the Node class in the MWindow sample:


        public IEnumerable<String> Comments {get; set;}    <br/>	<br/>        void ISourceInfo.SetSourceInfo(SourceContext sourceContext)
            {
                ISourceLocation loc = sourceContext.SourceLocation;
                SourceLocation.CopySourceLocation(loc, this as ISourceInfo);

                List<ParseToken> interleaveList = sourceContext.GetInterleave("Comment");
                if (interleaveList != null)
                {
                    var currentComments = Comments ?? Enumerable.Empty<string>();
                    var comments = new List<string>();
             
                    foreach (ParseToken token in interleaveList)
                    {
                        string comment = loc.ToString() + ", \"" + token.Text + "\"";                                                
                        
                        if (!currentComments.Contains(comment))
                        {
                            comments.Add(comment);
                            Console.WriteLine(comment);
                        }
                    }
                    
                    comments.AddRange(currentComments);
                    
                    if (comments.Count > 0)
                        Comments = comments;
                }
            }            
    
SourceContext.GetInterleave gets you into the interleave stream, and you get the location info as well. In this code, the comments are added as a collection on that node object.

Note that t he location info that comes through here is the location info in the output graph, not the input file.

I also noted with this particular implementation that at the Node level here, the only comments that came there were those contained within an element of the input code that corresponded to a node. I didn't get comments that I placed at the top or the bottom of the input. I suspect that this is due to where ISourceInfo is actually being implemented, but I don't have the cycles at the moment to investigate more deeply.

Does that give you more to go on?

.Kraig
  • Marked As Answer byCeyhun Ciper Friday, September 18, 2009 5:42 AM
  •  
Kraig Brockschmidt

Interesting question. Can you give me an example of what you're trying to accomplish, exactly?

.Kraig

Kraig Brockschmidt
Do you mean: i want to make a grammar that can understand comments and parse them to comments in the resulting MGraph? I think there are examples on how to do this.


Or did you mean the equivalent of the followingin (MGrammar to MGraph)?:
http://social.msdn.microsoft.com/Forums/en-US/oslo/thread/2e3b2de5-7c4d-4482-8bbd-1e6f8a42ca85

Performing my Final Project, looking into codename "Oslo".
Elger [Centric]
I need that for language translation (e.g. PL/SQL --> C#) where I would like to keep the comments at appropriate places. Another example is a T-SQL pretty printer (or beautifier).

-Ciper
Ceyhun Ciper
I mean, treat comments as whitespace but preserve them in the resulting MGraph; for instance, a beautifier for any language should format the comments and place them next to the corresponding production. One should not intersperse Comment constructs all over the place in the Grammar.

Unfortunately there is no easy way to do this and there are no examples at allin Oslo; Antlr handles this by havinga secondary "hidden" channel (you can think of it as a ghost MGraph superposed on the primary MGraph).

Maybe a workaround can be found through attributes? (like annotating ASTs)

-Ciper
Ceyhun Ciper
Thanks. I'm working on an answer for you with the "M" team--there is a way to tap into the interleave stream during parsing and get the source location and the comments. Seems like what you describe for Antlr. There are just some details I'm clarifying for you still.

.Kraig
  • Marked As Answer byCeyhun Ciper Thursday, September 17, 2009 5:12 AM
  •  
Kraig Brockschmidt
Thanks. I already mark this as an answer as I know that a solution/workaround will be found.

Regards,
-Ciper

ps: The tools (e.g DSL Grammar Mode of Intellipad)don't have to support it; just API support is plenty.
Ceyhun Ciper
Yes, Intellipad doesn't show comments in the output graph. But yes, the API does support this through the ISourceInfo interface. For example, I added this piece to the Node class in the MWindow sample:


        public IEnumerable<String> Comments {get; set;}    <br/>	<br/>        void ISourceInfo.SetSourceInfo(SourceContext sourceContext)
            {
                ISourceLocation loc = sourceContext.SourceLocation;
                SourceLocation.CopySourceLocation(loc, this as ISourceInfo);

                List<ParseToken> interleaveList = sourceContext.GetInterleave("Comment");
                if (interleaveList != null)
                {
                    var currentComments = Comments ?? Enumerable.Empty<string>();
                    var comments = new List<string>();
             
                    foreach (ParseToken token in interleaveList)
                    {
                        string comment = loc.ToString() + ", \"" + token.Text + "\"";                                                
                        
                        if (!currentComments.Contains(comment))
                        {
                            comments.Add(comment);
                            Console.WriteLine(comment);
                        }
                    }
                    
                    comments.AddRange(currentComments);
                    
                    if (comments.Count > 0)
                        Comments = comments;
                }
            }            
    
SourceContext.GetInterleave gets you into the interleave stream, and you get the location info as well. In this code, the comments are added as a collection on that node object.

Note that t he location info that comes through here is the location info in the output graph, not the input file.

I also noted with this particular implementation that at the Node level here, the only comments that came there were those contained within an element of the input code that corresponded to a node. I didn't get comments that I placed at the top or the bottom of the input. I suspect that this is due to where ISourceInfo is actually being implemented, but I don't have the cycles at the moment to investigate more deeply.

Does that give you more to go on?

.Kraig
  • Marked As Answer byCeyhun Ciper Friday, September 18, 2009 5:42 AM
  •  
Kraig Brockschmidt
Thanks; that's more than I expected!

And thank you very much for taking the time to work-out a sample.

-Ciper

ps: Top and bottom comments are trivial to handle anyhow so it isn't an issue.
Ceyhun Ciper

You can use google to search for other answers

Custom Search

More Threads

• May 2009 CTP: Quadrant row insert does not work for types with constrained fields
• MGrammer: Token precedence help
• SQL Creation Error
• Formatting Document (Cntrl-E,D) causes error in Intellipad
• How to assign an extent to an field?
• MGrammer: Can parsed tokens be used to influence further parsing in syntax rules
• Internal Error: Operation is not valid due to the current state of the object.(System.InvalidOperationException) at MX
• Example from MIX09 presentation issue
• Do .mx images get loaded into repository?
• How to process an XAML file against the MSchema?