summaryrefslogtreecommitdiffstats
path: root/dom/xslt/xpath/txExprLexer.h
blob: 9d3ca2810ed52160f1bfb6d30a037eca80b264d5 (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
190
191
192
193
194
195
196
197
198
199
200
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef MITREXSL_EXPRLEXER_H
#define MITREXSL_EXPRLEXER_H

#include "txCore.h"
#include "nsString.h"

/**
 * A Token class for the ExprLexer.
 *
 * This class was ported from XSL:P, an open source Java based
 * XSLT processor, written by yours truly.
 */
class Token {
 public:
  /**
   * Token types
   */
  enum Type {
    //-- Trivial Tokens
    NULL_TOKEN = 1,
    LITERAL,
    NUMBER,
    CNAME,
    VAR_REFERENCE,
    PARENT_NODE,
    SELF_NODE,
    R_PAREN,
    R_BRACKET,  // 9
    /**
     * start of tokens for 3.7, bullet 1
     * ExprLexer::nextIsOperatorToken bails if the tokens aren't
     * consecutive.
     */
    COMMA,
    AT_SIGN,
    L_PAREN,
    L_BRACKET,
    AXIS_IDENTIFIER,

    // These tokens include their following left parenthesis
    FUNCTION_NAME_AND_PAREN,  // 15
    COMMENT_AND_PAREN,
    NODE_AND_PAREN,
    PROC_INST_AND_PAREN,
    TEXT_AND_PAREN,

    /**
     * operators
     */
    //-- boolean ops
    AND_OP,  // 20
    OR_OP,

    //-- relational
    EQUAL_OP,  // 22
    NOT_EQUAL_OP,
    LESS_THAN_OP,
    GREATER_THAN_OP,
    LESS_OR_EQUAL_OP,
    GREATER_OR_EQUAL_OP,
    //-- additive operators
    ADDITION_OP,  // 28
    SUBTRACTION_OP,
    //-- multiplicative
    DIVIDE_OP,  // 30
    MULTIPLY_OP,
    MODULUS_OP,
    //-- path operators
    PARENT_OP,  // 33
    ANCESTOR_OP,
    UNION_OP,
    /**
     * end of tokens for 3.7, bullet 1 -/
     */
    //-- Special endtoken
    END  // 36
  };

  /**
   * Constructors
   */
  using iterator = nsAString::const_char_iterator;

  Token(iterator aStart, iterator aEnd, Type aType)
      : mStart(aStart), mEnd(aEnd), mType(aType), mNext(nullptr) {}
  Token(iterator aChar, Type aType)
      : mStart(aChar), mEnd(aChar + 1), mType(aType), mNext(nullptr) {}

  const nsDependentSubstring Value() { return Substring(mStart, mEnd); }

  iterator mStart, mEnd;
  Type mType;
  Token* mNext;
};

/**
 * A class for splitting an "Expr" String into tokens and
 * performing  basic Lexical Analysis.
 *
 * This class was ported from XSL:P, an open source Java based XSL processor
 */

class txExprLexer {
 public:
  txExprLexer();
  ~txExprLexer();

  /**
   * Parse the given string.
   * returns an error result if lexing failed.
   * The given string must outlive the use of the lexer, as the
   * generated Tokens point to Substrings of it.
   * mPosition points to the offending location in case of an error.
   */
  nsresult parse(const nsAString& aPattern);

  using iterator = nsAString::const_char_iterator;
  iterator mPosition;

  /**
   * Functions for iterating over the TokenList
   */

  Token* nextToken();
  Token* peek() {
    NS_ASSERTION(mCurrentItem, "peek called uninitialized lexer");
    return mCurrentItem;
  }
  Token* peekAhead() {
    NS_ASSERTION(mCurrentItem, "peekAhead called on uninitialized lexer");
    // Don't peek past the end node
    return (mCurrentItem && mCurrentItem->mNext) ? mCurrentItem->mNext
                                                 : mCurrentItem;
  }
  bool hasMoreTokens() {
    NS_ASSERTION(mCurrentItem, "HasMoreTokens called on uninitialized lexer");
    return (mCurrentItem && mCurrentItem->mType != Token::END);
  }

  /**
   * Trivial Tokens
   */
  //-- LF, changed to enum
  enum _TrivialTokens {
    D_QUOTE = '\"',
    S_QUOTE = '\'',
    L_PAREN = '(',
    R_PAREN = ')',
    L_BRACKET = '[',
    R_BRACKET = ']',
    L_ANGLE = '<',
    R_ANGLE = '>',
    COMMA = ',',
    PERIOD = '.',
    ASTERISK = '*',
    FORWARD_SLASH = '/',
    EQUAL = '=',
    BANG = '!',
    VERT_BAR = '|',
    AT_SIGN = '@',
    DOLLAR_SIGN = '$',
    PLUS = '+',
    HYPHEN = '-',
    COLON = ':',
    //-- whitespace tokens
    SPACE = ' ',
    TX_TAB = '\t',
    TX_CR = '\n',
    TX_LF = '\r'
  };

 private:
  Token* mCurrentItem;
  Token* mFirstItem;
  Token* mLastItem;

  int mTokenCount;

  void addToken(Token* aToken);

  /**
   * Returns true if the following Token should be an operator.
   * This is a helper for the first bullet of [XPath 3.7]
   *  Lexical Structure
   */
  bool nextIsOperatorToken(Token* aToken);

  /**
   * Returns true if the given character represents a numeric letter (digit)
   * Implemented in ExprLexerChars.cpp
   */
  static bool isXPathDigit(char16_t ch) { return (ch >= '0' && ch <= '9'); }
};

#endif