parser: use control_verb parser inline

This commit is contained in:
Justin Viiret 2017-02-02 15:49:26 +11:00 committed by Matthew Barr
parent bfc8be5675
commit bef6889844
6 changed files with 28 additions and 20 deletions

View File

@ -560,13 +560,13 @@ unichar readUtf8CodePoint4c(const char *s) {
throw LocatedParseError("(*UCP) must be at start of " throw LocatedParseError("(*UCP) must be at start of "
"expression, encountered"); "expression, encountered");
}; };
'UTF16)' => { # Use the control verb mini-parser to report an error for this
throw LocatedParseError("(*UTF16) not supported"); # unsupported/unknown verb.
}; [^)]+ ')' => {
'UTF32)' => { ParseMode temp_mode;
throw LocatedParseError("(*UTF32) not supported"); assert(ts - 2 >= ptr); // parser needs the '(*' at the start too.
}; read_control_verbs(ts - 2, te, (ts - 2 - ptr), temp_mode);
any => { assert(0); // Should have thrown a parse error.
throw LocatedParseError("Unknown control verb"); throw LocatedParseError("Unknown control verb");
}; };
*|; *|;
@ -1838,7 +1838,7 @@ unique_ptr<Component> parse(const char *ptr, ParseMode &globalMode) {
// First, read the control verbs, set any global mode flags and move the // First, read the control verbs, set any global mode flags and move the
// ptr forward. // ptr forward.
p = read_control_verbs(p, pe, globalMode); p = read_control_verbs(p, pe, 0, globalMode);
const char *eof = pe; const char *eof = pe;
int cs; int cs;

View File

@ -34,11 +34,13 @@
#ifndef CONTROL_VERBS_H #ifndef CONTROL_VERBS_H
#define CONTROL_VERBS_H #define CONTROL_VERBS_H
#include "ue2common.h"
namespace ue2 { namespace ue2 {
struct ParseMode; struct ParseMode;
const char *read_control_verbs(const char *ptr, const char *end, const char *read_control_verbs(const char *ptr, const char *end, size_t start,
ParseMode &mode); ParseMode &mode);
} // namespace ue2 } // namespace ue2

View File

@ -43,7 +43,7 @@ using namespace std;
namespace ue2 { namespace ue2 {
const char *read_control_verbs(const char *ptr, const char *end, const char *read_control_verbs(const char *ptr, const char *end, size_t start,
ParseMode &mode) { ParseMode &mode) {
const char *p = ptr; const char *p = ptr;
const char *pe = end; const char *pe = end;
@ -108,7 +108,7 @@ const char *read_control_verbs(const char *ptr, const char *end,
%% write exec; %% write exec;
} catch (LocatedParseError &error) { } catch (LocatedParseError &error) {
if (ts >= ptr && ts <= pe) { if (ts >= ptr && ts <= pe) {
error.locate(ts - ptr); error.locate(ts - ptr + start);
} else { } else {
error.locate(0); error.locate(0);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, Intel Corporation * Copyright (c) 2015-2017, Intel Corporation
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
@ -44,9 +44,13 @@ ParseError::~ParseError() {}
LocatedParseError::~LocatedParseError() {} LocatedParseError::~LocatedParseError() {}
void LocatedParseError::locate(size_t offset) { void LocatedParseError::locate(size_t offset) {
if (finalized) {
return;
}
std::ostringstream str; std::ostringstream str;
str << reason << " at index " << offset << "."; str << reason << " at index " << offset << ".";
reason = str.str(); reason = str.str();
finalized = true;
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, Intel Corporation * Copyright (c) 2015-2017, Intel Corporation
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
@ -30,8 +30,8 @@
* \brief Parse/Compile exceptions. * \brief Parse/Compile exceptions.
*/ */
#ifndef PARSE_ERROR_H_A02047D1AA16C9 #ifndef PARSE_ERROR_H
#define PARSE_ERROR_H_A02047D1AA16C9 #define PARSE_ERROR_H
#include "util/compile_error.h" #include "util/compile_error.h"
@ -44,22 +44,24 @@ class ParseError : public CompileError {
public: public:
// Note: 'why' should describe why the error occurred and end with a // Note: 'why' should describe why the error occurred and end with a
// full stop, but no line break. // full stop, but no line break.
explicit ParseError(const std::string &why) : CompileError(why) {} explicit ParseError(std::string why) : CompileError(std::move(why)) {}
~ParseError() override; ~ParseError() override;
}; };
class LocatedParseError : public ParseError { class LocatedParseError : public ParseError {
public: public:
explicit LocatedParseError(const std::string &why) : ParseError(".") { explicit LocatedParseError(std::string why) : ParseError(".") {
reason = why; // don't use ParseError ctor reason = std::move(why); // don't use ParseError ctor
} }
~LocatedParseError() override; ~LocatedParseError() override;
void locate(size_t offset); void locate(size_t offset);
private:
bool finalized = false; //!< true when locate() has been called.
}; };
} // namespace ue2 } // namespace ue2
#endif /* PARSE_ERROR_H_A02047D1AA16C9 */ #endif /* PARSE_ERROR_H */

View File

@ -92,7 +92,7 @@
93:/a\o{777}/ #Value in \o{...} sequence is too large at index 1. 93:/a\o{777}/ #Value in \o{...} sequence is too large at index 1.
94:/(*UTF16)foo/ #Unsupported control verb (*UTF16) at index 0. 94:/(*UTF16)foo/ #Unsupported control verb (*UTF16) at index 0.
95:/(*BSR_UNICODE)abc/ #Unsupported control verb (*BSR_UNICODE) at index 0. 95:/(*BSR_UNICODE)abc/ #Unsupported control verb (*BSR_UNICODE) at index 0.
96:/a+(*SKIP)b/ #Unknown control verb at index 4. 96:/a+(*SKIP)b/ #Unknown control verb (*SKIP) at index 2.
97:/foo(*/ #Invalid repeat at index 4. 97:/foo(*/ #Invalid repeat at index 4.
98:/[:\]:]/ #POSIX named classes are only supported inside a class at index 0. 98:/[:\]:]/ #POSIX named classes are only supported inside a class at index 0.
99:/[[:[:]/ #Invalid POSIX named class at index 1. 99:/[[:[:]/ #Invalid POSIX named class at index 1.