diff --git a/fuzztest/grammars/JSON.g4 b/fuzztest/grammars/JSON.g4 index 818b1cae..f620fb3c 100644 --- a/fuzztest/grammars/JSON.g4 +++ b/fuzztest/grammars/JSON.g4 @@ -16,7 +16,7 @@ // // With some simplifications: // - Restricted character set (e.g., no unicode chars). -// - No escape sequences (e.g., \n, \t, \uff01, etc.) +// - No unicode escape sequences (e.g., \uff01) grammar JSON_GRAMMAR; @@ -36,10 +36,12 @@ elements : element (',' element)* ; element : value ; -STRING : '"' CHARACTER* '"' ; +STRING : '"' (CHARACTER | '\\' ESCAPED )* '"' ; CHARACTER : [a-zA-Z0-9_]; +ESCAPED : '"' | '\\' | '/' | 'b' | 'f' | 'n' | 'r' | 't' ; + NUMBER : INTEGER FRACTION? EXPONENT? ; INTEGER : DIGIT | ONETONINE DIGITS | '-' DIGIT | '-' ONETONINE DIGITS ; diff --git a/grammar_codegen/antlr_frontend.cc b/grammar_codegen/antlr_frontend.cc index 35edf357..65bde31a 100644 --- a/grammar_codegen/antlr_frontend.cc +++ b/grammar_codegen/antlr_frontend.cc @@ -43,7 +43,7 @@ void ChangeStringQuote(std::string& str) { std::string EscapeString(absl::string_view text) { std::string excape_text; for (int i = 0; i < text.size(); ++i) { - if (text[i] == '"' || text[i] == '\\') { + if (text[i] == '"') { excape_text.push_back('\\'); } excape_text.push_back(text[i]); diff --git a/grammar_codegen/testdata/expected_json_grammar.h b/grammar_codegen/testdata/expected_json_grammar.h index 3274ed6e..4cfd90d1 100644 --- a/grammar_codegen/testdata/expected_json_grammar.h +++ b/grammar_codegen/testdata/expected_json_grammar.h @@ -30,6 +30,7 @@ enum JsonTypes { kElementNode, kSTRINGNode, kCHARACTERNode, + kESCAPEDNode, kNUMBERNode, kINTEGERNode, kDIGITSNode, @@ -47,29 +48,38 @@ enum JsonTypes { kElementsSubNode5, kElementsSubNode6, kSTRINGSubNode7, - kNUMBERSubNode8, - kNUMBERSubNode9, - kINTEGERSubNode10, - kINTEGERSubNode11, + kSTRINGSubNode9, + kSTRINGSubNode8, + kNUMBERSubNode10, + kNUMBERSubNode11, kINTEGERSubNode12, - kDIGITSSubNode13, - kEXPONENTSubNode14, - kEXPONENTSubNode15, - kWSPACESubNode16, - kLiteral7, - kLiteral11, - kLiteral8, + kINTEGERSubNode13, + kINTEGERSubNode14, + kDIGITSSubNode15, + kEXPONENTSubNode16, + kEXPONENTSubNode17, + kWSPACESubNode18, + kLiteral14, + kLiteral18, + kLiteral15, + kLiteral13, kLiteral6, - kLiteral5, - kLiteral3, kLiteral12, + kLiteral3, + kLiteral19, kLiteral4, - kLiteral13, + kLiteral5, + kLiteral20, + kLiteral7, + kLiteral8, kLiteral1, - kLiteral2, - kLiteral0, kLiteral9, + kLiteral2, kLiteral10, + kLiteral11, + kLiteral0, + kLiteral16, + kLiteral17, kCharSet3, kCharSet1, kCharSet2, @@ -85,6 +95,7 @@ class ElementsNode; class ElementNode; class STRINGNode; class CHARACTERNode; +class ESCAPEDNode; class NUMBERNode; class INTEGERNode; class DIGITSNode; @@ -102,48 +113,64 @@ class ArraySubNode4; class ElementsSubNode5; class ElementsSubNode6; class STRINGSubNode7; -class NUMBERSubNode8; -class NUMBERSubNode9; -class INTEGERSubNode10; -class INTEGERSubNode11; +class STRINGSubNode9; +class STRINGSubNode8; +class NUMBERSubNode10; +class NUMBERSubNode11; class INTEGERSubNode12; -class DIGITSSubNode13; -class EXPONENTSubNode14; -class EXPONENTSubNode15; -class WSPACESubNode16; -class Literal7; -class Literal11; -class Literal8; +class INTEGERSubNode13; +class INTEGERSubNode14; +class DIGITSSubNode15; +class EXPONENTSubNode16; +class EXPONENTSubNode17; +class WSPACESubNode18; +class Literal14; +class Literal18; +class Literal15; +class Literal13; class Literal6; -class Literal5; -class Literal3; class Literal12; +class Literal3; +class Literal19; class Literal4; -class Literal13; +class Literal5; +class Literal20; +class Literal7; +class Literal8; class Literal1; -class Literal2; -class Literal0; class Literal9; +class Literal2; class Literal10; +class Literal11; +class Literal0; +class Literal16; +class Literal17; class CharSet3; class CharSet1; class CharSet2; class CharSet0; -inline constexpr absl::string_view kStrLiteral7 = "+"; -inline constexpr absl::string_view kStrLiteral11 = ","; -inline constexpr absl::string_view kStrLiteral8 = "-"; -inline constexpr absl::string_view kStrLiteral6 = "."; -inline constexpr absl::string_view kStrLiteral5 = "0"; +inline constexpr absl::string_view kStrLiteral14 = "+"; +inline constexpr absl::string_view kStrLiteral18 = ","; +inline constexpr absl::string_view kStrLiteral15 = "-"; +inline constexpr absl::string_view kStrLiteral13 = "."; +inline constexpr absl::string_view kStrLiteral6 = "/"; +inline constexpr absl::string_view kStrLiteral12 = "0"; inline constexpr absl::string_view kStrLiteral3 = ":"; -inline constexpr absl::string_view kStrLiteral12 = "["; +inline constexpr absl::string_view kStrLiteral19 = "["; inline constexpr absl::string_view kStrLiteral4 = "\""; -inline constexpr absl::string_view kStrLiteral13 = "]"; +inline constexpr absl::string_view kStrLiteral5 = "\\"; +inline constexpr absl::string_view kStrLiteral20 = "]"; +inline constexpr absl::string_view kStrLiteral7 = "b"; +inline constexpr absl::string_view kStrLiteral8 = "f"; inline constexpr absl::string_view kStrLiteral1 = "false"; +inline constexpr absl::string_view kStrLiteral9 = "n"; inline constexpr absl::string_view kStrLiteral2 = "null"; +inline constexpr absl::string_view kStrLiteral10 = "r"; +inline constexpr absl::string_view kStrLiteral11 = "t"; inline constexpr absl::string_view kStrLiteral0 = "true"; -inline constexpr absl::string_view kStrLiteral9 = "{"; -inline constexpr absl::string_view kStrLiteral10 = "}"; +inline constexpr absl::string_view kStrLiteral16 = "{"; +inline constexpr absl::string_view kStrLiteral17 = "}"; inline constexpr absl::string_view kStrCharSet3 = R"grammar([ \t\n\r])grammar"; inline constexpr absl::string_view kStrCharSet1 = R"grammar([1-9])grammar"; inline constexpr absl::string_view kStrCharSet2 = R"grammar([Ee])grammar"; @@ -168,73 +195,99 @@ class ElementNode final : public TupleDomain {}; class STRINGNode final : public TupleDomain {}; class CHARACTERNode final : public TupleDomain {}; +class ESCAPEDNode final + : public VariantDomain { +}; class NUMBERNode final : public TupleDomain {}; + NUMBERSubNode10, NUMBERSubNode11> { +}; class INTEGERNode final - : public VariantDomain {}; -class DIGITSNode final : public TupleDomain {}; + : public VariantDomain {}; +class DIGITSNode final : public TupleDomain {}; class DIGITNode final - : public VariantDomain {}; + : public VariantDomain {}; class ONETONINENode final : public TupleDomain {}; class FRACTIONNode final - : public TupleDomain {}; + : public TupleDomain {}; class EXPONENTNode final - : public TupleDomain {}; -class SIGNNode final : public VariantDomain { -}; -class WSPACENode final : public TupleDomain {}; + : public TupleDomain {}; +class SIGNNode final + : public VariantDomain {}; +class WSPACENode final : public TupleDomain {}; class ObjectSubNode0 final - : public TupleDomain {}; + : public TupleDomain {}; class ObjectSubNode1 final - : public TupleDomain {}; + : public TupleDomain {}; class MembersSubNode2 final - : public TupleDomain { + : public TupleDomain { }; class ArraySubNode3 final - : public TupleDomain {}; + : public TupleDomain {}; class ArraySubNode4 final - : public TupleDomain {}; + : public TupleDomain {}; class ElementsSubNode5 final : public Vector {}; class ElementsSubNode6 final - : public TupleDomain {}; -class STRINGSubNode7 final : public Vector {}; -class NUMBERSubNode8 final : public Optional {}; -class NUMBERSubNode9 final : public Optional {}; -class INTEGERSubNode10 final - : public TupleDomain {}; -class INTEGERSubNode11 final - : public TupleDomain {}; -class INTEGERSubNode12 final : public TupleDomain {}; +class STRINGSubNode7 final : public Vector {}; +class STRINGSubNode9 final + : public TupleDomain {}; +class STRINGSubNode8 final + : public VariantDomain { +}; +class NUMBERSubNode10 final : public Optional { +}; +class NUMBERSubNode11 final : public Optional { +}; +class INTEGERSubNode12 final + : public TupleDomain {}; +class INTEGERSubNode13 final + : public TupleDomain {}; +class INTEGERSubNode14 final : public TupleDomain {}; -class DIGITSSubNode13 final - : public NonEmptyVector {}; -class EXPONENTSubNode14 final : public Optional { +class DIGITSSubNode15 final + : public NonEmptyVector {}; +class EXPONENTSubNode16 final : public Optional { }; -class EXPONENTSubNode15 final : public Optional { +class EXPONENTSubNode17 final : public Optional { }; -class WSPACESubNode16 final - : public NonEmptyVector {}; -class Literal7 final : public StringLiteralDomain {}; -class Literal11 final : public StringLiteralDomain { +class WSPACESubNode18 final + : public NonEmptyVector {}; +class Literal14 final : public StringLiteralDomain { +}; +class Literal18 final : public StringLiteralDomain { +}; +class Literal15 final : public StringLiteralDomain { +}; +class Literal13 final : public StringLiteralDomain { }; -class Literal8 final : public StringLiteralDomain {}; class Literal6 final : public StringLiteralDomain {}; -class Literal5 final : public StringLiteralDomain {}; -class Literal3 final : public StringLiteralDomain {}; class Literal12 final : public StringLiteralDomain { }; +class Literal3 final : public StringLiteralDomain {}; +class Literal19 final : public StringLiteralDomain { +}; class Literal4 final : public StringLiteralDomain {}; -class Literal13 final : public StringLiteralDomain { +class Literal5 final : public StringLiteralDomain {}; +class Literal20 final : public StringLiteralDomain { }; +class Literal7 final : public StringLiteralDomain {}; +class Literal8 final : public StringLiteralDomain {}; class Literal1 final : public StringLiteralDomain {}; -class Literal2 final : public StringLiteralDomain {}; -class Literal0 final : public StringLiteralDomain {}; class Literal9 final : public StringLiteralDomain {}; +class Literal2 final : public StringLiteralDomain {}; class Literal10 final : public StringLiteralDomain { }; +class Literal11 final : public StringLiteralDomain { +}; +class Literal0 final : public StringLiteralDomain {}; +class Literal16 final : public StringLiteralDomain { +}; +class Literal17 final : public StringLiteralDomain { +}; class CharSet3 final : public RegexLiteralDomain {}; class CharSet1 final : public RegexLiteralDomain {}; class CharSet2 final : public RegexLiteralDomain {};