summaryrefslogtreecommitdiff
path: root/xml/relaxng/src/org/intellij/plugins/relaxNG/compact/parser/PatternParsing.java
blob: 7fff0c3d0627609b5eca3ba5e3413e9b2adbaf30 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright 2007 Sascha Weinreuter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.intellij.plugins.relaxNG.compact.parser;

import com.intellij.lang.PsiBuilder;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.containers.ContainerUtil;
import org.intellij.plugins.relaxNG.compact.RncElementTypes;

import java.util.Map;

import static org.intellij.plugins.relaxNG.compact.RncTokenTypes.*;

/**
 * Created by IntelliJ IDEA.
* User: sweinreuter
* Date: 09.08.2007
*/
public class PatternParsing extends DeclarationParsing {

  @SuppressWarnings({"unchecked"})
  protected static final Map<IElementType, IElementType> TOKEN_MAP =
    ContainerUtil.newIdentityTroveMap();

  static {
    TOKEN_MAP.put(COMMA, RncElementTypes.SEQUENCE);
    TOKEN_MAP.put(PIPE, RncElementTypes.CHOICE);
    TOKEN_MAP.put(AND, RncElementTypes.INTERLEAVE);
    TOKEN_MAP.put(STAR, RncElementTypes.ZERO_OR_MORE);
    TOKEN_MAP.put(PLUS, RncElementTypes.ONE_OR_MORE);
    TOKEN_MAP.put(QUEST, RncElementTypes.OPTIONAL);
  }

  private final NameClassParsing myNameClassParsing;

  public PatternParsing(PsiBuilder builder) {
    super(builder);
    myNameClassParsing = new NameClassParsing(builder);
  }

  public void parse() {
    parseTopLevel();
  }

  protected boolean parsePattern() {
    PsiBuilder.Marker marker = myBuilder.mark();
    if (!parseQuantifiedPattern()) {
      marker.drop();
      return false;
    }

    while (BINARY_OPS.contains(currentToken())) {
      IElementType t;
      if (BINARY_OPS.contains(t = currentToken())) {
        do {
          advance();
          if (!parseQuantifiedPattern()) {
            error("Pattern expected");
          }
        } while (currentToken() == t);
        marker.done(TOKEN_MAP.get(t));
        marker = marker.precede();
      }
    }
    marker.drop();
    return true;
  }

  private boolean parseQuantifiedPattern() {
    final PsiBuilder.Marker marker = myBuilder.mark();
    if (!parseSinglePattern()) {
      marker.drop();
      return false;
    }

    final IElementType t = currentToken();
    if (matches(QUANTIFIER_OPS)) {
      marker.done(TOKEN_MAP.get(t));
    } else {
      marker.drop();
    }
    return true;
  }

  private boolean parseSinglePattern() {
    final PsiBuilder.Marker marker = myBuilder.mark();
    if (matches(ATTR_OR_ELEMENT)) {
      if (!myNameClassParsing.parseNameClass()) {
        error("Name class expected");
        marker.drop();
        return false;
      }
      parseBracedPattern();
      marker.done(RncElementTypes.PATTERN);
    } else if (matches(KEYWORD_LIST)) {
      parseBracedPattern();
      marker.done(RncElementTypes.LIST_PATTERN);
    } else if (matches(KEYWORD_MIXED)) {
      parseBracedPattern();
      marker.done(RncElementTypes.MIXED_PATTERN);
    } else if (matches(KEYWORD_EXTERNAL)) {
      parseAnyUriLiteral();
      parseInherit();
      marker.done(RncElementTypes.EXTERNAL_REF);
    } else if (matches(KEYWORD_NOT_ALLOWED)) {
      marker.done(RncElementTypes.NOT_ALLOWED_PATTERN);
    } else if (matches(KEYWORD_TEXT)) {
      marker.done(RncElementTypes.TEXT_PATTERN);
    } else if (matches(KEYWORD_EMPTY)) {
      marker.done(RncElementTypes.EMPTY_PATTERN);
    } else if (matches(KEYWORD_PARENT)) {
      match(IDENTIFIERS, "Identifier expected");
      marker.done(RncElementTypes.PARENT_REF);
    } else if (matches(KEYWORD_GRAMMAR)) {
      parseBracedGrammarContents();
      marker.done(RncElementTypes.GRAMMAR_PATTERN);
    } else if (matches(LPAREN)) {
      if (!parsePattern()) {
        error("Pattern expected");
      }
      match(RPAREN, "')' expected");
      marker.done(RncElementTypes.GROUP_PATTERN);
    } else if (matches(IDENTIFIERS)) {
      marker.done(RncElementTypes.REF_PATTERN);
    } else if (matches(LA_DATATYPE)) {
      parseDatatype();
      marker.done(RncElementTypes.DATATYPE_PATTERN);
    } else if (currentToken() == PREFIXED_NAME) {
      makeName();
      parseDatatype();
      marker.done(RncElementTypes.DATATYPE_PATTERN);
    } else if (matches(LITERAL)) {
      marker.done(RncElementTypes.DATATYPE_PATTERN);
    } else {
      marker.drop();
      return false;
    }
    return true;
  }

  private void parseDatatype() {
    if (currentToken() == LITERAL) {
      advance();
    } else if (matches(LBRACE)) {
      parseParams();
      if (matches(MINUS)) {
        if (!parsePattern()) {
          error("Pattern expected");
        }
      }
      match(RBRACE, "'}' expected");
    }
  }

  private void parseBracedPattern() {
    match(LBRACE, "'{' expected");
    if (!parsePattern()) {
      error("Pattern expected");
    }
    match(RBRACE, "'}' expected");
  }

  private void parseParams() {
    final IElementType t = currentToken();
    if (t != RBRACE) {
      do {
        final PsiBuilder.Marker marker = myBuilder.mark();
        match(IDENTIFIER_OR_KEYWORD, "Identifier expected");
        match(EQ, "'=' expected");
        match(LITERAL, "Literal expected");
        marker.done(RncElementTypes.PARAM);
      } while (IDENTIFIER_OR_KEYWORD.contains(currentToken()));
    }
  }
}