XQuery Syntax Language

SyntaxEditor for Windows Forms Forum

Posted 10 years ago by Simon Sprott
Version: 9.1.0505
Avatar
Hi

I'm trying to implement an XQuery editor. I'm most of the way there, but the embedded XML is causing me problems.

The language allows XML to be placed within it. The rules basically mean an XML section has to have paired element markers.

so you can have
let $row := 8   | XQuery Scope
<a>             | Change to XML Scope
<b>
</b>
</a>            | Return to Previous Scope
let $row := 8   
as an added complication you can jump back into XQuery scope using {} brackets
let $row := 8   | XQuery Scope
<a>             | Change to XML Scope
{               | Change to XQuery Scope
}               | Return to Previous Scope
<b xx="{some XQuery Expression}>
</b>
</a>            | Return to Previous Scope
let $row := 8   |
I've been trying to use the standard XML langauge as a merge langauge, but this doesn't track the count of open elements. Is there an easy way to make this work?

Another issue is that the '<' token is somewhat ambiguous in the lexer scope as it can 'mean less' than or 'element start' token. I can resolve this 99% of the time using a lookahead on for the XML token ie "{QName}", but its not ideal.

I have code to produce my own lexer and grammar parser which produces an AST, although the lexer really needs the interaction with the grammar parser to work properly.

I was looking at the PerformLexicalParse & PerformSemanticParse methods and wondering if I could use my AST to implement these methods, but it looks like this will take a fair bit of coding, and I don't want to start down that route without knowing theres light at the end of the tunnel.

Any advice would be very welcome

Regards
Simon



An Example of the XQuery Syntax.
declare function tour:list-possible-moves (
                $board as xs:integer*,
                $square as xs:integer )
            as xs:integer* 
{
    let $row as xs:integer := $square idiv 8
    let $column as xs:integer := $square mod 8

    return
        if ($row < 1)
            then $square - 17 else ()
};

declare function tour:print-board (
                $board as xs:integer* )
            as element()
{
    (: Output the board in HTML format :)

    <html>
    <head>
        <title>Knight's tour</title>
    </head>
    <body>
    <div align="center">
    <h1>Knight's tour starting at {$start}</h1>
    <table border="1" cellpadding="4">
        {for $row in 0 to 7 return
           <tr>
              {for $column in 0 to 7
                let $color :=
                          if ((($row + $column) mod 2)=1)
                          then 'xffff44' 
                          else 'white' return
                <td align="center" bgcolor="{$color}" width="22">{
                  let $n := $board[$row * 8 + $column + 1]
                  return 
                      if ($endd != 64 and $n = $endd)
                      then <b>{$n}</b>
                      else if ($n = 0)
                      then "&#xa0;"
                      else $n
                }</td>
              }
           </tr>
        }
    </table>
    <p>{
        if ($endd != 64) 
        then
          <a href="Tour?start={$start}&amp;end={$endd+1}">Step</a>
        else ()
    }</p>    
    </div>
    </body>
    </html>
};

Comments (2)

Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Simon,

First, you posted this in the SyntaxEditor for WPF forum but I'm assuming you mean the WinForms version (based on the methods you mentioned) instead, right? I can move it to the other forum if that is the case.

Unfortunately this sort of language is very difficult to program since it does require a semantic element for the lexing to work successfully. I don't think it would be possible to implement this with the dynamic language lexical parser due to the tag balance requirement. You would definitely need a hardcoded lexical parser to make this work.

I think it would be possible however you'd probably want to make a non-mergable language that is capable of handling both the XML and query states. You'd need your tokens (via a custom TokenBase-based implementation) to persist a linked list of parent elements, etc. too. This way for incremental parsing you'd be able to restore the stack of elements and query contexts that you have been through at a given point. We have an old sample of making a non-mergable language and lexer we could send you if you think it would help. But you'd need to take those concepts, and augment it with tracking all the query state/element stack data. Not an easy task but should be doable with some effort.


Actipro Software Support

Posted 10 years ago by Simon Sprott
Avatar
Your right, I was looking at the winforms version...ooops.

I was thinking this was probably going to be an issue, as its taken a lot of fiddling about with the lexx and grammar scripts to get this working. Not the best language, a few points of ambiguity and tricky context switching...

If you have an example of using a custom tokenize then this would give me some idea of the problem, and allow me to make a call on how to approach it.

I think it would take a bit of work to make my parser incremental, as its not something I'd even considered before. If you have any sample or hints that may help with that side of things, I'd also be interested (i'm using gplex and gppg).

Thanks for the quick response

Simon
The latest build of this product (v2018.1 build 0341) was released 6 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.