aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/main/javacc/StateParser.jj
blob: aaf6c1d6b4640f9f4ffa2b739919de5b380c50a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * A system state parser.
 * When this file is changed, do "ant compileparser" to rebuild the parser.
 *
 * @author Simon Thoresen Hult
 * @version $Id: StateParser.jj,v 1.7 2007-11-15 13:24:45 simon Exp $
 */
options {
    CACHE_TOKENS  = true;
    STATIC = false;
    DEBUG_PARSER = false;
    IGNORE_CASE = true;

    // Flip for higher performance
    ERROR_REPORTING = true;
}

PARSER_BEGIN(StateParser)

package com.yahoo.documentapi.messagebus.systemstate.parser;

import com.yahoo.documentapi.messagebus.systemstate.rule.*;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.ArrayList;

public class StateParser {

}

PARSER_END(StateParser)

TOKEN :
{
    <WHITESPACE: " " | "\t" | "\r" | "\n" | "\f"> |
    <SLASH: "/"> |
    <DOTDOT: ".."> |
    <DOT: "."> |
    <ARG: "?"> |
    <AND: "&"> |
    <EQ: "="> |
    <STRING: (<SPACE> | <CODE> | <ALPHANUM>)+> |
        <#SPACE: "+"> |
        <#CODE: "%" ["0"-"9","A"-"F","a"-"f"] ["0"-"9","A"-"F","a"-"f"]> |
        <#ALPHANUM: ["0"-"9","A"-"Z","a"-"z","-",".","_","~"]>
}

NodeState systemState() throws UnsupportedEncodingException :
{
    NodeState node = new NodeState();
    Location loc, pwd = new Location();
    List<Argument> arg;
}
{
     ( ( <WHITESPACE> )*
       ( loc = location(pwd)              { arg = null; }
         [ <ARG> arg = argumentList() ] ) { if (arg == null) { pwd = new Location(loc); }
                                            else { node.addChild(loc.toString(), new NodeState(arg)); } } )+
     { return node; }
}

Location location(Location pwd) throws UnsupportedEncodingException :
{
    String item;
    List<String> list = new ArrayList<String>();
}
{
    ( ( <SLASH> { pwd.getItems().clear(); } )?
      item = locationItem() { list.add(item); }
      ( LOOKAHEAD(2) <SLASH> item = locationItem() { list.add(item); } )*
      ( LOOKAHEAD(2) <SLASH> )? )
    { Location ret = new Location(pwd, list); return ret; }
}

String locationItem() throws UnsupportedEncodingException :
{
    String ret;
}
{
    ( <DOTDOT> { return NodeState.NODE_PARENT; } |
      <DOT>    { return NodeState.NODE_CURRENT; } |
      <STRING> { return URLDecoder.decode(token.image, "UTF-8"); } )
}

List<Argument> argumentList () throws UnsupportedEncodingException :
{
    Argument item;
    List<Argument> list = new ArrayList<Argument>();
}
{
    ( item = argument() { list.add(item); } ( <AND> item = argument() { list.add(item); } )* )
    { return list; }
}

Argument argument() throws UnsupportedEncodingException :
{
    String key, val;
}
{
    ( <STRING> { key = token.image; } <EQ>
      <STRING> { val = token.image; } )
    { return new Argument(URLDecoder.decode(key, "UTF-8"), URLDecoder.decode(val, "UTF-8")); }
}