tiny.smg.tree.scanner.javascript
Defined in tree.scanner.
API (3)
Types and contracts
Public types and contracts.
Source
Source: tools/smg/src/tree/scanner/javascript.zig
zig
const std = @import("std");const ecma = @import("ecma/root.zig");pub const Token = enum(u16) { automatic_semicolon, template_chars, ternary_qmark, html_comment, logical_or, escape_sequence, regex_pattern, jsx_text,};pub const Valid = packed struct(u8) { automatic_semicolon: bool = false, template_chars: bool = false, ternary_qmark: bool = false, html_comment: bool = false, logical_or: bool = false, escape_sequence: bool = false, regex_pattern: bool = false, jsx_text: bool = false,};const WhitespaceResult = enum { reject, no_newline, accept,};pub const Scanner = struct { pub fn scan(_: *Scanner, lexer: anytype, valid: Valid) bool { if (valid.template_chars) { if (valid.automatic_semicolon) return false; return ecma.scanTemplateChars(lexer, @backingInt(Token.template_chars)); } if (valid.jsx_text and ecma.scanJsxText(lexer, @backingInt(Token.jsx_text))) return true; if (valid.automatic_semicolon) { var scanned_comment = false; const result = scanAutomaticSemicolon(lexer, !valid.logical_or, &scanned_comment); if (!result and !scanned_comment and valid.ternary_qmark and lexer.lookahead() == '?') { return scanTernaryQmark(lexer); } return result; } if (valid.ternary_qmark) return scanTernaryQmark(lexer); if (valid.html_comment and !valid.logical_or and !valid.escape_sequence and !valid.regex_pattern) { return ecma.scanHtmlComment(lexer, @backingInt(Token.html_comment)); } return false; } pub fn serialize(_: Scanner, _: []u8) usize { return 0; } pub fn deserialize(_: *Scanner, serialized: []const u8) void { std.debug.assert(serialized.len == 0); }};fn scanWhitespaceAndComments(lexer: anytype, scanned_comment: *bool, consume: bool) WhitespaceResult { var saw_block_newline = false; while (true) { while (ecma.isSpace(lexer.lookahead())) lexer.advance(true); if (lexer.lookahead() != '/') return .accept; lexer.advance(true); if (lexer.lookahead() == '/') { lexer.advance(true); while (lexer.lookahead() != 0 and !ecma.isNewline(lexer.lookahead())) lexer.advance(true); scanned_comment.* = true; } else if (lexer.lookahead() == '*') { lexer.advance(true); while (lexer.lookahead() != 0) { if (lexer.lookahead() == '*') { lexer.advance(true); if (lexer.lookahead() == '/') { lexer.advance(true); scanned_comment.* = true; if (lexer.lookahead() != '/' and !consume) { return if (saw_block_newline) .accept else .no_newline; } break; } } else if (ecma.isNewline(lexer.lookahead())) { saw_block_newline = true; lexer.advance(true); } else { lexer.advance(true); } } } else { return .reject; } }}fn scanAutomaticSemicolon(lexer: anytype, comment_condition: bool, scanned_comment: *bool) bool { lexer.setResult(@backingInt(Token.automatic_semicolon)); lexer.markEnd(); while (true) { if (lexer.lookahead() == 0) return true; if (lexer.lookahead() == '/') { const result = scanWhitespaceAndComments(lexer, scanned_comment, false); if (result == .reject) return false; if (result == .accept and comment_condition and lexer.lookahead() != ',' and lexer.lookahead() != '=') { return true; } } if (lexer.lookahead() == '}') return true; if (lexer.includedRangeStart()) return true; if (ecma.isNewline(lexer.lookahead())) break; if (!ecma.isSpace(lexer.lookahead())) return false; lexer.advance(true); } lexer.advance(true); if (scanWhitespaceAndComments(lexer, scanned_comment, true) == .reject) return false; switch (lexer.lookahead()) { '`', ',', ':', ';', '*', '%', '>', '<', '=', '[', '(', '?', '^', '|', '&', '/' => return false, '.' => { lexer.advance(true); return ecma.isDigit(lexer.lookahead()); }, '+' => { lexer.advance(true); return lexer.lookahead() == '+'; }, '-' => { lexer.advance(true); return lexer.lookahead() == '-'; }, '!' => { lexer.advance(true); return lexer.lookahead() != '='; }, 'i' => { lexer.advance(true); if (lexer.lookahead() != 'n') return true; lexer.advance(true); if (!ecma.isAlpha(lexer.lookahead())) return false; for ("stanceof") |letter| { if (lexer.lookahead() != letter) return true; lexer.advance(true); } if (!ecma.isAlpha(lexer.lookahead())) return false; }, else => {}, } return true;}fn scanTernaryQmark(lexer: anytype) bool { while (ecma.isSpace(lexer.lookahead())) lexer.advance(true); if (lexer.lookahead() != '?') return false; lexer.advance(false); if (lexer.lookahead() == '?') return false; lexer.markEnd(); lexer.setResult(@backingInt(Token.ternary_qmark)); if (lexer.lookahead() == '.') { lexer.advance(false); return ecma.isDigit(lexer.lookahead()); } return true;}Source: tools/smg/src/tree/scanner/root.zig:2
zig
pub const javascript = @import("javascript.zig");Audit
| Definitions | 3 |
|---|---|
| Public names | 3 |
| Members | 16 |
| Version | 26.7.0 |
| Revision | daab053ee433 |