Qt 4.8
Classes | Public Types | Public Functions | Protected Functions | Protected Variables | List of all members
QScript::SyntaxChecker Class Reference

#include <qscriptsyntaxchecker_p.h>

Inheritance diagram for QScript::SyntaxChecker:
QScriptGrammar

Classes

struct  Result
 

Public Types

enum  State { Error, Intermediate, Valid }
 

Public Functions

Result checkSyntax (const QString &code)
 
 SyntaxChecker ()
 
 ~SyntaxChecker ()
 

Protected Functions

bool automatic (QScript::Lexer *lexer, int token) const
 
void reallocateStack ()
 

Protected Variables

int stack_size
 
int * state_stack
 
int tos
 

Additional Inherited Members

- Protected Types inherited from QScriptGrammar
enum  {
  EOF_SYMBOL = 0, T_AND = 1, T_AND_AND = 2, T_AND_EQ = 3,
  T_AUTOMATIC_SEMICOLON = 62, T_BREAK = 4, T_CASE = 5, T_CATCH = 6,
  T_COLON = 7, T_COMMA = 8, T_CONST = 81, T_CONTINUE = 9,
  T_DEBUGGER = 82, T_DEFAULT = 10, T_DELETE = 11, T_DIVIDE_ = 12,
  T_DIVIDE_EQ = 13, T_DO = 14, T_DOT = 15, T_ELSE = 16,
  T_EQ = 17, T_EQ_EQ = 18, T_EQ_EQ_EQ = 19, T_FALSE = 80,
  T_FINALLY = 20, T_FOR = 21, T_FUNCTION = 22, T_GE = 23,
  T_GT = 24, T_GT_GT = 25, T_GT_GT_EQ = 26, T_GT_GT_GT = 27,
  T_GT_GT_GT_EQ = 28, T_IDENTIFIER = 29, T_IF = 30, T_IN = 31,
  T_INSTANCEOF = 32, T_LBRACE = 33, T_LBRACKET = 34, T_LE = 35,
  T_LPAREN = 36, T_LT = 37, T_LT_LT = 38, T_LT_LT_EQ = 39,
  T_MINUS = 40, T_MINUS_EQ = 41, T_MINUS_MINUS = 42, T_NEW = 43,
  T_NOT = 44, T_NOT_EQ = 45, T_NOT_EQ_EQ = 46, T_NULL = 78,
  T_NUMERIC_LITERAL = 47, T_OR = 48, T_OR_EQ = 49, T_OR_OR = 50,
  T_PLUS = 51, T_PLUS_EQ = 52, T_PLUS_PLUS = 53, T_QUESTION = 54,
  T_RBRACE = 55, T_RBRACKET = 56, T_REMAINDER = 57, T_REMAINDER_EQ = 58,
  T_RESERVED_WORD = 83, T_RETURN = 59, T_RPAREN = 60, T_SEMICOLON = 61,
  T_STAR = 63, T_STAR_EQ = 64, T_STRING_LITERAL = 65, T_SWITCH = 66,
  T_THIS = 67, T_THROW = 68, T_TILDE = 69, T_TRUE = 79,
  T_TRY = 70, T_TYPEOF = 71, T_VAR = 72, T_VOID = 73,
  T_WHILE = 74, T_WITH = 75, T_XOR = 76, T_XOR_EQ = 77,
  ACCEPT_STATE = 237, RULE_COUNT = 269, STATE_COUNT = 468, TERMINAL_COUNT = 84,
  NON_TERMINAL_COUNT = 88, GOTO_INDEX_OFFSET = 468, GOTO_INFO_OFFSET = 1562, GOTO_CHECK_OFFSET = 1562
}
 
- Static Protected Functions inherited from QScriptGrammar
static int nt_action (int state, int nt)
 
static int t_action (int state, int token)
 
- Static Protected Variables inherited from QScriptGrammar
static const short action_check []
 
static const short action_default []
 
static const short action_index []
 
static const short action_info []
 
static const short goto_default []
 
static const short lhs []
 
static const short rhs []
 
static const int rule_index []
 
static const int rule_info []
 
static const char *const spell []
 

Detailed Description

Definition at line 48 of file qscriptsyntaxchecker_p.h.

Enumerations

◆ State

Constructors and Destructors

◆ SyntaxChecker()

QScript::SyntaxChecker::SyntaxChecker ( )

Definition at line 34 of file qscriptsyntaxchecker.cpp.

◆ ~SyntaxChecker()

QScript::SyntaxChecker::~SyntaxChecker ( )

Definition at line 41 of file qscriptsyntaxchecker.cpp.

42 {
43  if (stack_size) {
45  }
46 }
Q_CORE_EXPORT void qFree(void *ptr)
Definition: qmalloc.cpp:58

Functions

◆ automatic()

bool QScript::SyntaxChecker::automatic ( QScript::Lexer lexer,
int  token 
) const
protected

Definition at line 48 of file qscriptsyntaxchecker.cpp.

Referenced by checkSyntax().

49 {
50  return token == T_RBRACE || token == 0 || lexer->prevTerminator();
51 }
bool prevTerminator() const

◆ checkSyntax()

SyntaxChecker::Result QScript::SyntaxChecker::checkSyntax ( const QString code)

Definition at line 53 of file qscriptsyntaxchecker.cpp.

Referenced by QScriptEnginePrivate::canEvaluate(), and QScriptEnginePrivate::checkSyntax().

54 {
55  const int INITIAL_STATE = 0;
56  QScript::Lexer lexer (/*engine=*/ 0);
57  lexer.setCode(code, /*lineNo*/ 1);
58 
59  int yytoken = -1;
60  int saved_yytoken = -1;
61  QString error_message;
62  int error_lineno = -1;
63  int error_column = -1;
64  State checkerState = Valid;
65 
67 
68  tos = 0;
69  state_stack[++tos] = INITIAL_STATE;
70 
71  while (true)
72  {
73  const int state = state_stack [tos];
74  if (yytoken == -1 && - TERMINAL_COUNT != action_index [state])
75  {
76  if (saved_yytoken == -1)
77  yytoken = lexer.lex();
78  else
79  {
80  yytoken = saved_yytoken;
81  saved_yytoken = -1;
82  }
83  }
84 
85  int act = t_action (state, yytoken);
86 
87  if (act == ACCEPT_STATE) {
88  if (lexer.error() == QScript::Lexer::UnclosedComment)
89  checkerState = Intermediate;
90  else
91  checkerState = Valid;
92  break;
93  } else if (act > 0) {
94  if (++tos == stack_size)
96 
97  state_stack [tos] = act;
98  yytoken = -1;
99  }
100 
101  else if (act < 0)
102  {
103  int r = - act - 1;
104 
105  tos -= rhs [r];
106  act = state_stack [tos++];
107 
109  || (r == Q_SCRIPT_REGEXPLITERAL_RULE2)) {
110  // Skip the rest of the RegExp literal
111  bool rx = lexer.scanRegExp();
112  if (!rx) {
113  checkerState = Intermediate;
114  break;
115  }
116  }
117 
118  state_stack [tos] = nt_action (act, lhs [r] - TERMINAL_COUNT);
119  }
120 
121  else
122  {
123  if (saved_yytoken == -1 && automatic (&lexer, yytoken) && t_action (state, T_AUTOMATIC_SEMICOLON) > 0)
124  {
125  saved_yytoken = yytoken;
126  yytoken = T_SEMICOLON;
127  continue;
128  }
129 
130  else if ((state == INITIAL_STATE) && (yytoken == 0)) {
131  // accept empty input
132  yytoken = T_SEMICOLON;
133  continue;
134  }
135 
136  int ers = state;
137  int shifts = 0;
138  int reduces = 0;
139  int expected_tokens [3];
140  for (int tk = 0; tk < TERMINAL_COUNT; ++tk)
141  {
142  int k = t_action (ers, tk);
143 
144  if (! k)
145  continue;
146  else if (k < 0)
147  ++reduces;
148  else if (spell [tk])
149  {
150  if (shifts < 3)
151  expected_tokens [shifts] = tk;
152  ++shifts;
153  }
154  }
155 
156  error_message.clear ();
157  if (shifts && shifts < 3)
158  {
159  bool first = true;
160 
161  for (int s = 0; s < shifts; ++s)
162  {
163  if (first)
164  error_message += QLatin1String ("Expected ");
165  else
166  error_message += QLatin1String (", ");
167 
168  first = false;
169  error_message += QLatin1Char('`');
170  error_message += QLatin1String (spell [expected_tokens [s]]);
171  error_message += QLatin1Char('\'');
172  }
173  }
174 
175  if (error_message.isEmpty())
176  error_message = lexer.errorMessage();
177 
178  error_lineno = lexer.startLineNo();
179  error_column = lexer.startColumnNo();
180  checkerState = Error;
181  break;
182  }
183  }
184 
185  if (checkerState == Error) {
186  if (lexer.error() == QScript::Lexer::UnclosedComment)
187  checkerState = Intermediate;
188  else if (yytoken == 0)
189  checkerState = Intermediate;
190  }
191  return Result(checkerState, error_lineno, error_column, error_message);
192 }
static const short lhs[]
#define Q_SCRIPT_REGEXPLITERAL_RULE1
static int t_action(int state, int token)
static int nt_action(int state, int nt)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
#define Q_SCRIPT_REGEXPLITERAL_RULE2
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool automatic(QScript::Lexer *lexer, int token) const
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The State element defines configurations of objects and properties.
static const short action_index[]
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
static const char *const spell[]
static const short rhs[]
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ reallocateStack()

void QScript::SyntaxChecker::reallocateStack ( )
inlineprotected

Definition at line 82 of file qscriptsyntaxchecker_p.h.

Referenced by checkSyntax().

83 {
84  if (! stack_size)
85  stack_size = 128;
86  else
87  stack_size <<= 1;
88 
89  state_stack = reinterpret_cast<int*> (qRealloc(state_stack, stack_size * sizeof(int)));
90 }
Q_CORE_EXPORT void * qRealloc(void *ptr, size_t size)
Definition: qmalloc.cpp:63

Properties

◆ stack_size

int QScript::SyntaxChecker::stack_size
protected

Definition at line 78 of file qscriptsyntaxchecker_p.h.

Referenced by checkSyntax(), reallocateStack(), and ~SyntaxChecker().

◆ state_stack

int* QScript::SyntaxChecker::state_stack
protected

Definition at line 79 of file qscriptsyntaxchecker_p.h.

Referenced by checkSyntax(), reallocateStack(), and ~SyntaxChecker().

◆ tos

int QScript::SyntaxChecker::tos
protected

Definition at line 77 of file qscriptsyntaxchecker_p.h.

Referenced by checkSyntax().


The documentation for this class was generated from the following files: