.NET Framework Bookmark and Share   
 index > Microsoft Codename 'Oslo' > MGrammar - projecting to a boolean
 

MGrammar - projecting to a boolean

I have a grammar that has a couple keywords that are optional. My MSchema wants a bool value - true if the keyword is there, false if the keyword is absent.

(not that I'm even close to my grammar generating M that is compatible with any schema, but that's the goal - though I begin to suspect any meaningful DSL syntax can't be translated directly to a relational schema using mgx.exe)

So I have this token:

@{Classification["Keyword"]}
token ReadOnly = "ReadOnly"
=> true;

And this syntax:

syntax SimpleProperty =
scope:Scope ident:Identity? ro:ReadOnly? type:Token name:Token EndStatement
=> Property { Name = name, Scope =scope, Identity=ident, ReadOnly = ro, Type = type };

Notice that ReadOnly is optional. This sort of renders into M, but not really.

If the keyword is there, the M output is 'true' including the ticks - which is not acceptible.

If the keyword is not there, the M output is null without any ticks, which is ok, but would require that this column allow nulls even though I really just want true/false.

I know I could probably solve this by having two syntax statements - one with the ReadOnly keyword and one without. But that's a really silly solution, because I have more than one optional keyword, so I'd end up with around 8 syntax blocks in my grammar, tons and tons of duplicated code just to handle this one issue.

Clearly this is a common scenario in languages. So I assume there must be a reasonable answer- probably something simple I'm missing.
Rockford Lhotka

Hi Rocky,
The short answer is that there isn't a way currently to eliminate quotes from values in the grammar output, but this is something that we are working on.

If you are trying to dump this into a database or the Repository, then you are going to need to fix up the M generated by MGX. There are a couple of options:
1. Create an import model with string fields and migrate the values to the 'real' model from there
2. Write a program with a regex to strip the quotes from the M file.

We are working on a sample for the second option.

BTW - It would be GREAT to get this entered as a bug in Connect so that others can vote on it.


Richard Orr

richorr
I blogged my code that does the fixup of the mgx output to solve both the boolean value issue, and another issue with single-item lists where the resulting M is entirely invalid.

http://www.lhotka.net/weblog/MCslaFixingUpOutputFromMgx.aspx
Rockford Lhotka
To answer myself (with some help from Justin Chase), a partial answer is this:

@{Classification["Keyword"]}
token ReadOnly = "ReadOnly";

syntax Boolean(e) =
e
=> true | empty => false;

And then to use this in my main syntax:

syntax SimpleProperty =
scope:Scope ro:Boolean(ReadOnly) type:Token name:Token EndStatement
=> Property { Name = name, Scope =scope, ReadOnly = ro, Type = type };


But I still get the single quotes around the value after using mgx.exe:

ReadOnly = 'false',

And those quotes cause issues with the M parser.
Rockford Lhotka

Hi Rocky,
The short answer is that there isn't a way currently to eliminate quotes from values in the grammar output, but this is something that we are working on.

If you are trying to dump this into a database or the Repository, then you are going to need to fix up the M generated by MGX. There are a couple of options:
1. Create an import model with string fields and migrate the values to the 'real' model from there
2. Write a program with a regex to strip the quotes from the M file.

We are working on a sample for the second option.

BTW - It would be GREAT to get this entered as a bug in Connect so that others can vote on it.


Richard Orr

richorr
Rockford Lhotka
I blogged my code that does the fixup of the mgx output to solve both the boolean value issue, and another issue with single-item lists where the resulting M is entirely invalid.

http://www.lhotka.net/weblog/MCslaFixingUpOutputFromMgx.aspx
Rockford Lhotka
I could have sworn we had fixed that bug, but I just tested your grammar against today's build and sure enough the quotes are there.

There obviously shouldn't be quotes around boolean literals.

Digging into it from our side.

DB
Don Box - Microsoft

You can use google to search for other answers

Custom Search

More Threads

• Disambiguator
• Details on MGrammar Attributes ?
• .mgx creation too slow
• Problem with final tokens in context
• How can one preserve comments in MGrammar?
• New version of Intellipad ?
• Getting started.
• Dealing with the Visual Studio 2010 expiration in the WCF and WF 4.0, “Oslo? & “Dublin?PDC08 Virtual Machine
• Let m.exe generate only DML
• Component Model Instances vs. Component Consumer Model Instances