xml.modes

1 Overview

This is a base set of modes for XML, documented in ../../doc/gloss-xml1.html and subsequent pages and also (in dues course) here, for more technical information.

These modes for GLOSS-XML can be extended by the usual mechanism. of hooks. Indeed many other sets of modes hook onto these base modes. Being small and simple it should be easier to extend and maintain.

2 The code

!declare-prefix @prefix[xml] @uri[http://gloss.bham.ac.uk/mv/xml/xml] {

!init-parameter @name[xml:document-type] @value[document]
!init-parameter @name[xml:text-buffer] @value[]
!init-parameter @name[xml:attr-value] @value[]
!init-parameter @name[xml:attr-found] @value[]

!init-parameter @name[xml:inner-structured-content] @value[xml:structured-content]
!init-parameter @name[xml:inner-document-fragment-mode] @value[xml:structured-content]
!init-parameter @name[xml:inner-dtd-mode] @value[xml:dtd-content]
!init-parameter @name[xml:default-prefix] @value[]
!init-parameter @name[xml:pelt-default-prefix] @value[]

!mode @name[xml:main] @accept[<!\[CDATA\[|<?gloss|<!--|] @type[process]
  !punc @value[<!\[CDATA\[] 
    !process-tokens @mode[xml:$\{xml:document-type\}] @use-indentation[false]
    !process-tokens @mode[xml:closing-cdata-tag] @use-indentation[false]
    !return
  !punc @value[<?gloss] 
    !process-tokens @mode[xml:$\{xml:document-type\}] @use-indentation[false]
    !process-tokens @mode[xml:closing-glosspi-tag] @use-indentation[false]
    !return
  !punc @value[<!--] 
    !process-tokens @mode[xml:$\{xml:document-type\}] @use-indentation[false]
    !process-tokens @mode[xml:closing-comment-tag] @use-indentation[false]
    !return
  !default 
    !process-tokens @mode[xml:$\{xml:document-type\}] @use-indentation[false]
    !return

!mode @name[xml:closing-cdata-tag] @accept[\]\]>|] @type[process]
  !punc @value[\]\]>] !return 
  !default 
    !message[ERROR$: a \]\]> was expected here, line $l, column $c[&#x0A;]]
    !return

!mode @name[xml:closing-glosspi-tag] @accept[?>|] @type[process]
  !punc @value[?>] !return 
  !default 
    !message[ERROR$: a ?> was expected here, line $l, column $c[&#x0A;]]
    !return

!mode @name[xml:closing-comment-tag] @accept[-->|] @type[process]
  !punc @value[-->] !return 
  !default 
    !message[ERROR$: a --> was expected here, line $l, column $c[&#x0A;]]
    !return

!mode @name[xml:document] @accept[] @type[process]
  !default 
    !document
      !if @test[$\{xml:bom\}] @value[include] !attribute @name[bom] [$\{xml:bom\}]
      !if @test[$\{xml:decl\}] @value[include] !attribute @name[xmldecl] [$\{xml:decl\}]
      !if @test[$\{xml:version\}] !attribute @name[version] [$\{xml:version\}]
      !if @test[$\{xml:encoding\}] !attribute @name[encoding] [$\{xml:encoding\}]
      !if @test[$\{xml:standalone\}] !attribute @name[standalone] [$\{xml:standalone\}]
      !process-tokens @mode[xml:doctype]
      !process-tokens @mode[xml:root]
    !return

!mode @name[xml:document-fragment] @accept[] @type[process]
  !default 
    !document-fragment
      !if @test[$\{xml:bom\}] @value[include] !attribute @name[bom] [$\{xml:bom\}]
      !if @test[$\{xml:decl\}] @value[include] !attribute @name[xmldecl] [$\{xml:decl\}]
      !if @test[$\{xml:version\}] !attribute @name[version] [$\{xml:version\}]
      !if @test[$\{xml:encoding\}] !attribute @name[encoding] [$\{xml:encoding\}]
      !process-tokens @mode[$\{xml:inner-document-fragment-mode\}]
    !return

!mode @name[xml:dtd] @accept[] @type[process]
  !default 
    !document-fragment
      !if @test[$\{xml:bom\}] @value[include] !attribute @name[bom] [$\{xml:bom\}]
      !if @test[$\{xml:decl\}] @value[include] !attribute @name[xmldecl] [$\{xml:decl\}]
      !if @test[$\{xml:version\}] !attribute @name[version] [$\{xml:version\}]
      !if @test[$\{xml:encoding\}] !attribute @name[encoding] [$\{xml:encoding\}]
      !process-tokens @mode[$\{xml:inner-dtd-mode\}]
    !return

!mode @name[xml:dtd-content] @accept[elt|attr|pelt|pi|cref|eref|!LITERAL|!|\{|\}|\[|\]] @type[process]
  !pelt
    !xr-element @name[$q]
      !process-tokens @mode[xml:dtd-content]
  !punc @data[\{]
    !execute @mode[xml:brace-group] 
      !set-parameter @name[xml:inner-structured-content] @value[xml:dtd-content]
  !punc @data[\}]
    !abort
  !include @mode[xml:structured-content]

!mode @name[xml:glosssrc-pi] @accept[] @type[process] @use-indentation[false]
  !default
    !processing-instruction @target[glosssrc] [doc="$s" line="$l" col="$c"]
    !return

!mode @name[xml:structured-content] @accept[elt|attr|pi|cref|eref|!CDATA|!LITERAL|pelt|!|\{|\}|\[|\]] @type[process]
  !pelt                              !execute @mode[xml:pelement]
  !elt                               !execute @mode[xml:element]
  !attr                              !execute @mode[xml:attribute]
  !pi @children-adjust[1]            !execute @mode[xml:pi]
  !cref                              !execute @mode[xml:reference]
  !eref                              !execute @mode[xml:reference]
  !punc @data[!CDATA]                !execute @mode[xml:cdata]
  !punc @data[!LITERAL]              !execute @mode[xml:literal]
  !punc @data[!] @children-adjust[1] !execute @mode[xml:comment]
  !punc @data[\{]                    !execute @mode[xml:brace-group]
  !punc @data[\}]                    !abort
  !punc @data[\[]                    !execute @mode[xml:text]
  !punc @data[\]]                    !abort

!mode @name[xml:doctype] @accept[!DOCTYPE|pi|!LITERAL|pelt|!] @children[1] @type[process]
  !pi @children-adjust[1] !execute @mode[xml:pi]
  !pelt !abort
  !punc @data[!LITERAL] @children-adjust[1] !execute @mode[xml:literal]
  !punc @data[!] @children-adjust[1] !execute @mode[xml:comment]
  !punc @data[!DOCTYPE]
    !doctype
      !process-tokens @mode[xml:attribute-list]
      !process-tokens @mode[xml:dtd-content]

!mode @name[xml:root] @accept[elt|pi|!LITERAL|pelt|!] @children[1] @type[process] @use-indentation[false]
  !pi @children-adjust[1] !execute @mode[xml:pi]
  !punc @data[!LITERAL] @children-adjust[1] !execute @mode[xml:literal]
  !punc @data[!] @children-adjust[1] !execute @mode[xml:comment]
  !elt  !execute @mode[xml:element]
  !pelt !execute @mode[xml:pelement]

!mode @name[xml:element] @accept[elt] @type[execute]
  !elt @prefix[]
    !process-tokens @mode[xml:glosssrc-pi]
    !if @test[$\{xml:default-prefix\}]
      !element @name[$\{xml:default-prefix\}$:$n]
        !process-tokens @mode[$\{xml:inner-structured-content\}]
    !else
      !element !process-tokens @mode[$\{xml:inner-structured-content\}]
  !elt
    !process-tokens @mode[xml:glosssrc-pi]
    !element !process-tokens @mode[$\{xml:inner-structured-content\}]

!mode @name[xml:pelement] @accept[pelt] @type[execute]
  !pelt @prefix[]
    !process-tokens @mode[xml:glosssrc-pi]
    !if @test[$\{xml:pelt-default-prefix\}]
      !element @name[$\{xml:pelt-default-prefix\}$:$n]
        !process-tokens @mode[$\{xml:inner-structured-content\}]
    !else
      !element !process-tokens @mode[$\{xml:inner-structured-content\}]
  !pelt
    !element !process-tokens @mode[$\{xml:inner-structured-content\}]

!mode @name[xml:process-attr] @accept[\[|] @children[1] @type[process]
  !punc !execute @mode[xml:get-attr]

!mode @name[xml:get-attr] @accept[\[|] @type[execute]
  !punc @data[\[]
    !set-parameter @name[xml:text-buffer] @value[]
    !process-tokens @mode[xml:get-attrchars] @parameters[share]
    !export-parameter @name[$\{xml:attr-param\}] @value[$\{xml:text-buffer\}]
  !punc @data[]
    !message [ERROR$: a \[ was expected here, line $l, column $c[&#x0A;]]
    !set-parameter @name[xml:text-buffer] @value[]
    !export-parameter @name[$\{xml:attr-param\}] @value[]

!mode @name[xml:get-attrchars] @accept[\\\\|\\\[|\\\]|\\\{|\\\}|\[|\]|\{|\}|uc|] @use-indentation[false] @type[process]
  !punc @data[\\\\] !append-parameter @name[xml:text-buffer] @value[\\]
  !punc @data[\\\[] !append-parameter @name[xml:text-buffer] @value[\[]
  !punc @data[\\\]] !append-parameter @name[xml:text-buffer] @value[\]]
  !punc @data[\\\{] !append-parameter @name[xml:text-buffer] @value[\{]
  !punc @data[\\\}] !append-parameter @name[xml:text-buffer] @value[\}]
  !uc               !append-parameter @name[xml:text-buffer] @value[$v]
  !punc @data[\{]   !message [ERROR$: a \{ is not allowed here, line $l, column $c[&#x0A;]]
  !punc @data[\}]   !message [ERROR$: a \} is not allowed here, line $l, column $c[&#x0A;]]
  !punc @data[\[]   !message [ERROR$: a \[ is not allowed here, line $l, column $c[&#x0A;]]
  !punc @data[\]]   !return
  !punc @data[]     
	!message [ERROR$: a \] was expected, line $l, column $c[&#x0A;]]
	!abort

!mode @name[xml:process-text] @accept[\[|] @children[1] @type[process]
  !punc !execute @mode[xml:text]

!mode @name[xml:text] @accept[\[|] @type[execute]
  !punc @data[\[]
    !process-tokens @mode[xml:text-content] 
      !set-parameter @name[xml:text-buffer] @value[]
    !process-tokens @mode[xml:check-close-bracket]
    !return
  !punc @data[]
    !message [ERROR$: a \[ was expected here, line $l, column $c[&#x0A;]]

!mode @name[xml:text-content] @accept[\\\\|\\\[|\\\]|\\\{|\\\}|\[|\]|\{|\}|uc|] @use-indentation[false] @type[process]
  !punc @data[\\\\] !append-parameter @name[xml:text-buffer] @value[\\]
  !punc @data[\\\[] !append-parameter @name[xml:text-buffer] @value[\[]
  !punc @data[\\\]] !append-parameter @name[xml:text-buffer] @value[\]]
  !punc @data[\\\{] !append-parameter @name[xml:text-buffer] @value[\{]
  !punc @data[\\\}] !append-parameter @name[xml:text-buffer] @value[\}]
  !uc               !append-parameter @name[xml:text-buffer] @value[$v]
  !punc @data[\{]   !message [ERROR$: a \{ is not allowed here, line $l, column $c[&#x0A;]]
  !punc @data[\}]   !message [ERROR$: a \} is not allowed here, line $l, column $c[&#x0A;]]
  !punc @data[\[] 
    !text [$\{xml:text-buffer\}]
    !set-parameter @name[xml:text-buffer] @value[]
    !process-tokens @mode[$\{xml:inner-structured-content\}] @use-indentation[false]
    !process-tokens @mode[xml:check-close-bracket]
  !punc @data[\]] 
    !text [$\{xml:text-buffer\}] 
    !abort
  !punc @data[]
    !message [ERROR$: a \] was expected, line $l, column $c[&#x0A;]]
    !abort

!mode @name[xml:brace-group] @accept[\{] @type[execute]
  !punc
    !process-tokens @mode[$\{xml:inner-structured-content\}] @use-indentation[false]
    !process-tokens @mode[xml:check-close-brace]
    !return    

!mode @name[xml:attribute-references] @accept[eref|cref|\]] @use-indentation[false] @type[process]
  !eref !execute @mode[xml:reference]
  !cref !execute @mode[xml:reference]
  !punc @data[\]] !return

!mode @name[xml:attribute-text] @accept[\\\\|\\\[|\\\]|\\\{|\\\}|\[|\]|\{|\}|uc] @use-indentation[false] @type[process]
  !punc @data[\\\\] !append-parameter @name[xml:text-buffer] @value[\\]
  !punc @data[\\\[] !append-parameter @name[xml:text-buffer] @value[\[]
  !punc @data[\\\]] !append-parameter @name[xml:text-buffer] @value[\]]
  !punc @data[\\\{] !append-parameter @name[xml:text-buffer] @value[\{]
  !punc @data[\\\}] !append-parameter @name[xml:text-buffer] @value[\}]
  !uc               !append-parameter @name[xml:text-buffer] @value[$v]
  !punc @data[\{]   !message [ERROR$: a \{ is not allowed here, line $l, column $c[&#x0A;]]
  !punc @data[\}]   !message [ERROR$: a \} is not allowed here, line $l, column $c[&#x0A;]]
  !punc @data[\[] 
    !text [$\{xml:text-buffer\}]
    !set-parameter @name[xml:text-buffer] @value[]
    !process-tokens @mode[xml:attribute-references] @use-indentation[false]
  !punc @data[\]] 
    !text [$\{xml:text-buffer\}] 
    !return

!mode @name[xml:attribute-value] @accept[\[|] @type[process]
  !punc @data[\[]
    !set-parameter @name[xml:text-buffer] @value[]
    !process-tokens @mode[xml:attribute-text] @use-indentation[false]
    !export-parameter @name[xml:attr-found] @value[yes]
    !return
  !default
    !return

!mode @name[xml:attribute-list] @accept[attr] @type[process]
  !attr !execute @mode[xml:attribute]

!mode @name[xml:attribute] @accept[attr] @type[execute]
  !attr
    !set-parameter @name[xml:attr-found] @value[]
    !set-parameter @name[xml:attr-value] @value[]
    !attribute @name[$q]
      !process-tokens @mode[xml:attribute-value]
      !if-not @test[$\{xml:attr-found\}] [$q]

!mode @name[xml:cdata] @accept[!CDATA] @type[execute]
  !default
    !cdata
      !process-tokens @mode[xml:process-text]

!mode @name[xml:literal] @accept[!LITERAL] @type[execute]
  !default
    !literal
      !process-tokens @mode[xml:process-text]

!mode @name[xml:comment] @accept[!] @type[execute]
  !default
    !comment
      !process-tokens @mode[xml:process-text]

!mode @name[xml:reference] @accept[cref|eref] @type[execute]
  !cref
    !character-reference
  !eref
    !entity-reference

!mode @name[xml:pi] @accept[pi] @type[execute]
  !default
    !processing-instruction
      !process-tokens @mode[xml:process-text]

!mode @name[xml:check-close-bracket] @accept[\]|] @use-indentation[false] @children[1] @type[process]
  !punc @data[\]] !return
  !default        !message [ERROR$: a \] was expected here, line $l, column $c[&#x0A;]]

!mode @name[xml:check-close-brace] @accept[\}|] @use-indentation[false] @children[1] @type[process]
  !punc @data[\}] !return
  !default        !message [ERROR$: a \} was expected here, line $l, column $c[&#x0A;]]

}; !declare-prefix

This file is part of the GLOSS system, Copyright Richard Kaye http://gloss.bham.ac.uk/. Usage permitted under the GPL. No Warranty.

This page is copyright. Web page design and creation by GLOSS.