svghmi/parse_labels.ysl2
branchwxPython4
changeset 3609 51a3d6f39944
parent 3595 375626e60b63
equal deleted inserted replaced
3608:02229133df43 3609:51a3d6f39944
     1 // parse_labels.ysl2
     1 // parse_labels.ysl2
     2 
     2 
     3 
     3 
     4 //  Parses:
     4 //  Parses:
     5 //  "HMI:WidgetType|freq:param1:param2@path1,path1min,path1max@path2"
     5 //  "HMI:WidgetType|freq:param1:param2@a=path1,path1min,path1max@b=path2#a+b>3"
     6 //
     6 //
     7 //  Into:
     7 //  Into:
     8 //  widget type="WidgetType" id="blah456" {
     8 //  widget type="WidgetType" id="blah456" {
     9 //      arg value="param1";
     9 //      arg value="param1";
    10 //      arg value="param2";
    10 //      arg value="param2";
    11 //      path value=".path1" index=".path1" min="path1min" max="path1max" type="PAGE_LOCAL";
    11 //      path value=".path1" index=".path1" min="path1min" max="path1max" type="PAGE_LOCAL";
    12 //      path value="/path1" index="348" type="HMI_INT";
    12 //      path value="/path1" index="348" type="HMI_INT";
    13 //      path value="path4" index="path4" type="HMI_LOCAL";
    13 //      path value="path4" index="path4" type="HMI_LOCAL";
    14 //  }
    14 //  }
    15 //
    15 //
    16 const "pathregex",!"'^([^\[,]+)(\[[^\]]+\])?([-.\d,]*)$'"!;
    16 const "pathregex",!"'^(\w+=)?([^,=]+)([-.\d,]*)$'"!;
    17 
    17 
    18 const "newline" |
    18 const "newline" |
    19 const "twonewlines", "concat($newline,$newline)";
    19 const "twonewlines", "concat($newline,$newline)";
    20 
    20 
    21 template "*", mode="parselabel"
    21 template "*", mode="parselabel"
    22 {
    22 {
    23     const "part","@inkscape:label";
    23     const "label","@inkscape:label";
    24     const "desc", "svg:desc";
    24     const "desc", "svg:desc";
    25 
    25 
    26     // add svg:desc field if continuation "\" marker is found at the end of label
    26     // add svg:desc field if continuation "\" marker is found at the end of label
    27     const "len","string-length($part)";
    27     const "len","string-length($label)";
    28     const "has_continuation", "substring($part,$len,1)='\\'";
    28     const "has_continuation", "substring($label,$len,1)='\\'";
    29     const "label" choose{
    29     const "full_decl" choose{
    30         when "$has_continuation" {
    30         when "$has_continuation" {
    31            const "_continuation", "substring-before($desc, $twonewlines)";
    31            const "_continuation", "substring-before($desc, $twonewlines)";
    32            const "continuation" choose {
    32            const "continuation" choose {
    33                when "$_continuation" value "$_continuation";
    33                when "$_continuation" value "$_continuation";
    34                otherwise value "$desc";
    34                otherwise value "$desc";
    35            }
    35            }
    36            value "concat(substring($part,1,$len - 1),translate($continuation,$newline,''))";
    36            value "concat(substring($label,1,$len - 1),translate($continuation,$newline,''))";
    37         }
    37         }
    38         otherwise value "$part";
    38         otherwise value "$label";
    39     }
    39     }
    40 
    40 
    41     const "id","@id";
    41     const "id","@id";
    42 
    42 
    43     const "description", "substring-after($label,'HMI:')";
    43     const "declaration", "substring-after($full_decl,'HMI:')";
    44 
    44 
    45     const "_args", "substring-before($description,'@')";
    45     const "_args", "substring-before($declaration,'@')";
    46     const "args" choose {
    46     const "args" choose {
    47         when "$_args" value "$_args";
    47         when "$_args" value "$_args";
    48         otherwise value "$description";
    48         otherwise value "$declaration";
    49     }
    49     }
    50 
    50 
    51     const "_typefreq", "substring-before($args,':')";
    51     const "_typefreq", "substring-before($args,':')";
    52     const "typefreq" choose {
    52     const "typefreq" choose {
    53         when "$_typefreq" value "$_typefreq";
    53         when "$_typefreq" value "$_typefreq";
    64     if "$type" widget {
    64     if "$type" widget {
    65         attrib "id" > «$id»
    65         attrib "id" > «$id»
    66         attrib "type" > «$type»
    66         attrib "type" > «$type»
    67         if "$freq" {
    67         if "$freq" {
    68             if "not(regexp:test($freq,'^[0-9]*(\.[0-9]+)?[smh]?'))" {
    68             if "not(regexp:test($freq,'^[0-9]*(\.[0-9]+)?[smh]?'))" {
    69                 error > Widget id:«$id» label:«$label» has wrong syntax of frequency forcing «$freq»
    69                 error > Widget id:«$id» label:«$full_decl» has wrong syntax of frequency forcing «$freq»
    70             }
    70             }
    71             attrib "freq" > «$freq»
    71             attrib "freq" > «$freq»
    72         }
    72         }
       
    73 
       
    74         // find "#" + JS expr at the end
       
    75         const "tail", "substring-after($declaration,'@')";
       
    76         const "taillen","string-length($tail)";
       
    77         const "has_enable", "contains($tail, '#')";
       
    78         const "paths" choose{
       
    79             when "$has_enable" {
       
    80                value "substring-before($tail,'#')";
       
    81             }
       
    82             otherwise value "$tail";
       
    83         }
       
    84         if "$has_enable" {
       
    85             const "enable_expr", "substring-after($tail,'#')";
       
    86             attrib "enable_expr" value "$enable_expr";
       
    87         }
       
    88 
    73         foreach "str:split(substring-after($args, ':'), ':')" {
    89         foreach "str:split(substring-after($args, ':'), ':')" {
    74             arg {
    90             arg {
    75                 attrib "value" > «.»
    91                 attrib "value" > «.»
    76             }
    92             }
    77         }
    93         }
    78         const "paths", "substring-after($description,'@')";
    94 
       
    95         // for stricter syntax checking, this should make error
       
    96         // if $paths contains "@@" or ends with "@" (empty paths)
       
    97 
    79         foreach "str:split($paths, '@')" {
    98         foreach "str:split($paths, '@')" {
    80             if "string-length(.) > 0" path {
    99             if "string-length(.) > 0" path {
    81                 // 1 : global match
   100                 // 1 : global match
       
   101                 // 2 : assign=
    82                 // 2 : /path
   102                 // 2 : /path
    83                 // 3 : [accepts]
   103                 // 3 : min,max
    84                 // 4 : min,max
       
    85                 const "path_match", "regexp:match(.,$pathregex)";
   104                 const "path_match", "regexp:match(.,$pathregex)";
       
   105                 const "pathassign", "substring-before($path_match[2],'=')";
    86                 const "pathminmax", "str:split($path_match[4],',')";
   106                 const "pathminmax", "str:split($path_match[4],',')";
    87                 const "path", "$path_match[2]";
   107                 const "path", "$path_match[3]";
    88                 const "path_accepts", "$path_match[3]";
       
    89                 const "pathminmaxcount", "count($pathminmax)";
   108                 const "pathminmaxcount", "count($pathminmax)";
    90                 attrib "value" > «$path»
   109                 if "not($path)"
    91                 if "string-length($path_accepts)"
   110                     error > Widget id:«$id» label:«$full_decl» has wrong syntax
    92                     attrib "accepts" > «$path_accepts»
   111 
       
   112                 attrib "value" value "$path";
       
   113                 if "$pathassign"
       
   114                     attrib "assign" value "$pathassign";
    93                 choose {
   115                 choose {
    94                     when "$pathminmaxcount = 2" {
   116                     when "$pathminmaxcount = 2" {
    95                         attrib "min" > «$pathminmax[1]»
   117                         attrib "min" > «$pathminmax[1]»
    96                         attrib "max" > «$pathminmax[2]»
   118                         attrib "max" > «$pathminmax[2]»
    97                     }
   119                     }
    98                     when "$pathminmaxcount = 1 or $pathminmaxcount > 2" {
   120                     when "$pathminmaxcount = 1 or $pathminmaxcount > 2" {
    99                         error > Widget id:«$id» label:«$label» has wrong syntax of path section «$pathminmax»
   121                         error > Widget id:«$id» label:«$full_decl» has wrong syntax of path section «$pathminmax»
   100                     }
   122                     }
   101                 }
   123                 }
   102                 if "$indexed_hmitree" choose {
   124                 if "$indexed_hmitree" choose {
   103                     when "regexp:test($path,'^\.[a-zA-Z0-9_]+$')" {
   125                     when "regexp:test($path,'^\.[a-zA-Z0-9_]+$')" {
   104                         attrib "type" > PAGE_LOCAL
   126                         attrib "type" > PAGE_LOCAL
   108                     }
   130                     }
   109                     otherwise {
   131                     otherwise {
   110                         const "item", "$indexed_hmitree/*[@hmipath = $path]";
   132                         const "item", "$indexed_hmitree/*[@hmipath = $path]";
   111                         const "pathtype", "local-name($item)";
   133                         const "pathtype", "local-name($item)";
   112                         if "$pathminmaxcount = 3 and not($pathtype = 'HMI_INT' or $pathtype = 'HMI_REAL')" {
   134                         if "$pathminmaxcount = 3 and not($pathtype = 'HMI_INT' or $pathtype = 'HMI_REAL')" {
   113                             error > Widget id:«$id» label:«$label» path section «$pathminmax» use min and max on non mumeric value
   135                             error > Widget id:«$id» label:«$full_decl» path section «$pathminmax» use min and max on non mumeric value
   114                         }
   136                         }
   115                         if "count($item) = 1" {
   137                         if "count($item) = 1" {
   116                             attrib "index" > «$item/@index»
   138                             attrib "index" > «$item/@index»
   117                             attrib "type" > «$pathtype»
   139                             attrib "type" > «$pathtype»
   118                         }
   140                         }