MCPcopy Index your code
hub / github.com/bazelbuild/bazel / stringLiteral

Method stringLiteral

src/main/java/net/starlark/java/syntax/Lexer.java:443–511  ·  view source on GitHub ↗

Scans a string literal delimited by 'quot'. ON ENTRY: 'pos' is 1 + the index of the first delimiter ON EXIT: 'pos' is 1 + the index of the last delimiter. @param isRaw if true, do not escape the string.

(char quot, boolean isRaw)

Source from the content-addressed store, hash-verified

441 * @param isRaw if true, do not escape the string.
442 */
443 private void stringLiteral(char quot, boolean isRaw) {
444 int literalStartPos = isRaw ? pos - 2 : pos - 1;
445 int contentStartPos = pos;
446
447 // Don't even attempt to parse triple-quotes here.
448 if (skipTripleQuote(quot)) {
449 pos -= 2;
450 escapedStringLiteral(quot, isRaw);
451 return;
452 }
453
454 // first quick optimistic scan for a simple non-escaped string
455 while (pos < buffer.length) {
456 char c = buffer[pos++];
457 switch (c) {
458 case '\n':
459 error("unclosed string literal", literalStartPos);
460 setToken(TokenKind.STRING, literalStartPos, pos);
461 setValue(bufferSlice(contentStartPos, pos - 1));
462 return;
463 case '\\':
464 if (isRaw) {
465 if (peek(0) == '\r' && peek(1) == '\n') {
466 // There was a CRLF after the newline. No shortcut possible, since it needs to be
467 // transformed into a single LF.
468 pos = contentStartPos;
469 escapedStringLiteral(quot, true);
470 return;
471 } else {
472 pos++;
473 break;
474 }
475 }
476 // oops, hit an escape, need to start over & build a new string buffer
477 pos = contentStartPos;
478 escapedStringLiteral(quot, false);
479 return;
480 case '\'':
481 case '"':
482 if (c == quot) {
483 // close-quote, all done.
484 setToken(TokenKind.STRING, literalStartPos, pos);
485 setValue(bufferSlice(contentStartPos, pos - 1));
486 // If we're requiring ASCII-only, do another scan for validation.
487 if (options.stringLiteralsAreAsciiOnly()) {
488 for (int i = contentStartPos; i < pos - 1; i++) {
489 if (buffer[i] >= 0x80) {
490 // Can report multiple errors per string literal.
491 error("string literal contains non-ASCII character", i);
492 }
493 }
494 }
495 return;
496 }
497 break;
498 default: // fall out
499 }
500 }

Callers 1

tokenizeMethod · 0.95

Calls 8

skipTripleQuoteMethod · 0.95
escapedStringLiteralMethod · 0.95
errorMethod · 0.95
setTokenMethod · 0.95
setValueMethod · 0.95
bufferSliceMethod · 0.95
peekMethod · 0.95

Tested by

no test coverage detected