Skip to content

Commit 97a8e29

Browse files
feat: add text block support in parser
1 parent 2b6c7e8 commit 97a8e29

4 files changed

Lines changed: 121 additions & 0 deletions

File tree

packages/java-parser/api.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,7 @@ export type LiteralCtx = {
671671
floatingPointLiteral?: FloatingPointLiteralCstNode[];
672672
booleanLiteral?: BooleanLiteralCstNode[];
673673
CharLiteral?: IToken[];
674+
TextBlock?: IToken[];
674675
StringLiteral?: IToken[];
675676
Null?: IToken[];
676677
};

packages/java-parser/src/productions/lexical-structure.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function defineRules($, t) {
77
{ ALT: () => $.SUBRULE($.floatingPointLiteral) },
88
{ ALT: () => $.SUBRULE($.booleanLiteral) },
99
{ ALT: () => $.CONSUME(t.CharLiteral) },
10+
{ ALT: () => $.CONSUME(t.TextBlock) },
1011
{ ALT: () => $.CONSUME(t.StringLiteral) },
1112
{ ALT: () => $.CONSUME(t.Null) }
1213
]);

packages/java-parser/src/tokens.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ createToken({
224224
"'(?:[^\\\\']|(?:(?:{{EscapeSequence}})|{{UnicodeInputCharacter}}))'"
225225
)
226226
});
227+
228+
createToken({
229+
name: "TextBlock",
230+
pattern: /"""\s*\n(\\"|\s|.)*?"""/
231+
});
232+
227233
createToken({
228234
name: "StringLiteral",
229235
pattern: MAKE_PATTERN('"(?:[^\\\\"]|{{StringCharacter}})*"')
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
"use strict";
2+
3+
const { expect } = require("chai");
4+
5+
const javaParser = require("../src/index");
6+
7+
describe("The Java Parser should parse TextBlocks", () => {
8+
it("should not parse a one word 'TextBlock'", () => {
9+
const input = `"""word"""`;
10+
11+
expect(() => javaParser.parse(input, "literal")).to.throw();
12+
});
13+
14+
it("should not parse a one word 'TextBlock' with starting spaces", () => {
15+
const input = `""" word"""`;
16+
17+
expect(() => javaParser.parse(input, "literal")).to.throw();
18+
});
19+
20+
it("should parse a one word 'TextBlock' starting with new line", () => {
21+
const input = `"""
22+
one"""`;
23+
24+
expect(() => javaParser.parse(input, "literal")).to.not.throw();
25+
});
26+
27+
it("should parse a one word 'TextBlock' starting with spaces and new line", () => {
28+
const input = `"""${" "}
29+
one"""`;
30+
31+
expect(() => javaParser.parse(input, "literal")).to.not.throw();
32+
});
33+
34+
it("should parse a one line 'TextBlock' starting with new line", () => {
35+
const input = `"""
36+
one sentence"""`;
37+
38+
expect(() => javaParser.parse(input, "literal")).to.not.throw();
39+
});
40+
41+
it("should parse a one line 'TextBlock' starting with spaces and new line", () => {
42+
const input = `"""${" "}
43+
one sentence"""`;
44+
45+
expect(() => javaParser.parse(input, "literal")).to.not.throw();
46+
});
47+
48+
it("should parse a multiline 'TextBlock'", () => {
49+
const input = `"""
50+
one word
51+
52+
sentence
53+
54+
"""`;
55+
56+
expect(() => javaParser.parse(input, "literal")).to.not.throw();
57+
});
58+
59+
it("should not parse a multiline 'TextBlock' which do not start with new line", () => {
60+
const input = `"""one word
61+
62+
sentence
63+
64+
"""`;
65+
66+
expect(() => javaParser.parse(input, "literal")).to.throw();
67+
});
68+
69+
it("should parse a multiline 'TextBlock' with ", () => {
70+
const input = `"""
71+
one word
72+
73+
"sentence"
74+
75+
"""`;
76+
77+
expect(() => javaParser.parse(input, "literal")).to.not.throw();
78+
});
79+
80+
it("should not parse a 'TextBlock' ending with 4x\"", () => {
81+
const input = `"""
82+
one word
83+
84+
"sentence"
85+
86+
""""`;
87+
88+
expect(() => javaParser.parse(input, "literal")).to.throw();
89+
});
90+
91+
it("should not parse a 'TextBlock' ending with 4x\"", () => {
92+
const input = `"""
93+
one word
94+
95+
"sentence"""
96+
97+
""`;
98+
99+
expect(() => javaParser.parse(input, "literal")).to.throw();
100+
});
101+
102+
it("should not parse a 'TextBlock' ending with 4x\"", () => {
103+
const input = `"""
104+
my text
105+
106+
107+
sentence\\"""
108+
109+
"""
110+
`;
111+
expect(() => javaParser.parse(input, "literal")).to.not.throw();
112+
});
113+
});

0 commit comments

Comments
 (0)