Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
sway
zig source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.14.0
zig 0.14.1
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
const std = @import("std"); pub const Position = struct { offset: u32, /// Create a `Position` at the start of a source file. pub fn zero() Position { return Position{ .offset = 0 }; } }; pub const Region = struct { start: Position, end: Position, /// Create a `Region` from raw offsets. pub fn from_raw_offsets(start: u32, end: u32) Region { std.debug.assert(start <= end); return Region{ .start = Position{ .offset = start }, .end = Position{ .offset = end }, }; } }; pub const Diagnostic = struct { tag: Tag, region: Region, /// Represents the type of diagnostic message. pub const Tag = enum { MisplacedCarriageReturn, AsciiControl, LeadingZero, UppercaseBase, InvalidUnicodeEscapeSequence, InvalidEscapeSequence, UnclosedString, UnclosedSingleQuote, OverClosedBrace, MismatchedBrace, NonPrintableUnicodeInStrLiteral, InvalidUtf8InSource, }; }; pub const Comment = struct { region: Region, pub fn init(begin: u32, end: u32) Comment { std.debug.assert(begin <= end); return Comment{ .region = Region.from_raw_offsets(begin, end) }; } }; pub const Cursor = struct { buf: []const u8, pos: u32, messages: []Diagnostic, message_count: u32, tab_width: u8 = 4, // TODO: make this configurable comment: ?Comment = null, /// Initialize a Cursor with the given input buffer and a pre-allocated messages slice. pub fn init(buf: []const u8, messages: []Diagnostic) Cursor { return Cursor{ .buf = buf, .pos = 0, .messages = messages, .message_count = 0, }; } fn pushMessageHere(self: *Cursor, tag: Diagnostic.Tag) void { self.pushMessage(tag, self.pos, self.pos); } fn pushMessage(self: *Cursor, tag: Diagnostic.Tag, begin: u32, end: u32) void { if (self.message_count < self.messages.len) { self.messages[self.message_count] = Diagnostic{ .tag = tag, .region = Region.from_raw_offsets(begin, end), }; } self.message_count += 1; } /// Chomps "trivia" (whitespace, comments, etc.) and returns an optional indent. /// If the chomped trivia includes a newline, returns the indent of the next (real) line. /// Otherwise, returns null. pub fn chompTrivia(self: *Cursor) ?u16 { var sawNewline = false; var indent: u16 = 0; while (self.pos < self.buf.len) { const b = self.buf[self.pos]; if (b == ' ') { self.pos += 1; if (sawNewline) indent += 1; } else if (b == '\t') { self.pos += 1; if (sawNewline) { // round up to the next tab stop indent = (indent + self.tab_width) & ~(self.tab_width - 1); } } else if (b == '\n') { self.pos += 1; sawNewline = true; indent = 0; return indent; } else if (b == '\r') { self.pos += 1; sawNewline = true; indent = 0; if (self.pos < self.buf.len and self.buf[self.pos] == '\n') { self.pos += 1; return indent; } else { self.pushMessageHere(.MisplacedCarriageReturn); } } else if (b == '#') { self.pos += 1; const comment_start = self.pos; while (self.pos < self.buf.len and self.buf[self.pos] != '\n' and self.buf[self.pos] != '\r') { self.pos += 1; } self.comment = Comment.init(comment_start, self.pos); } else if (b >= 0 and b <= 31) { self.pushMessageHere(.AsciiControl); self.pos += 1; } else { break; } } return null; } }; export fn chompTrivia(ptr: [*]const u8, len: usize) u16 { var messages: [128]Diagnostic = undefined; const msg_slice = messages[0..]; const buf: []const u8 = ptr[0..len]; var x = Cursor.init(buf, msg_slice); return x.chompTrivia() orelse 0; }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree