Skip to content

Commit 5ded4d9

Browse files
committed
Add documentation
1 parent 0fecd50 commit 5ded4d9

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/compiler.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44
use crate::{emitter::Emitter, parser::Parser, wat::Wat, Error, ErrorKind, Severity};
55
use alloc::{string::String, vec::Vec};
66

7+
/// Public interface to the C-to-WebAssembly compiler.
8+
///
9+
/// Same instance can be reused to compile multiple files.
710
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
811
pub struct Compiler;
912

1013
impl Compiler {
14+
/// Compiles the given C source to [WAT (WebAssembly Text) format](https://webassembly.github.io/mutable-global/core/text/index.html).
15+
///
16+
/// Call [Self::assemble] to produce a binary from the resulting WAT.
1117
pub fn compile(&mut self, source: impl AsRef<str>) -> Result<String, Error> {
1218
let mut parser = Parser::new(source.as_ref());
1319
let ast = parser.parse()?;
@@ -16,6 +22,9 @@ impl Compiler {
1622
Ok(wat)
1723
}
1824

25+
/// Compiles the given WAT source to [WebAssembly Binary format](https://webassembly.github.io/mutable-global/core/binary/index.html).
26+
///
27+
/// Call [Self::compile] to obtain a WAT source from the C source.
1928
#[cfg(feature = "binary-output")]
2029
pub fn assemble(&mut self, source: impl AsRef<str>) -> Result<Vec<u8>, Error> {
2130
use alloc::string::ToString;

src/error.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,104 @@ use crate::token::TokenKind;
55
use alloc::{rc::Rc, string::String};
66
use core::fmt::{self, Display, Formatter, Result};
77

8+
/// Kind of error encountered during compilation.
89
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
910
pub enum Kind {
11+
/// Invalid token, with the given offending character.
1012
InvalidToken(char),
13+
/// Invalid constant value.
1114
InvalidConstant,
15+
/// Constant value exceeded the capacity of a 32-bit `int`.
1216
ConstantTooLarge,
17+
/// Unknown type encountered, with the given token.
1318
UnknownType(TokenKind),
19+
/// Expected a type but none was found.
1420
ExpectedType,
21+
/// Expected an identifier but the given token was found instead (or `None` on EOF).
1522
ExpectedIdentifier(Option<TokenKind>),
23+
/// Expected a specific token but another given token was found instead (or `None` on EOF).
1624
ExpectedToken(TokenKind, Option<TokenKind>),
25+
/// Expected an expression but the given token was found instead (or `None` on EOF).
1726
ExpectedExpression(Option<TokenKind>),
27+
/// Encountered a redefinition of an identifier.
1828
Redefinition(Rc<String>),
29+
/// Attempted to use an undeclared identifier.
1930
Undeclared(Rc<String>),
31+
/// Conflicting type definitions for the same identifier.
2032
ConflictingTypes(Rc<String>),
33+
/// Invalid left-hand value in an assignment.
2134
InvalidLvalue,
35+
/// `break` outside of a loop or `switch` statement.
2236
BreakOutsideLoopOrSwitch,
37+
/// `continue` outside of a loop.
2338
ContinueOutsideLoop,
39+
/// Attempted to call an undefined function.
2440
UndefinedFunction(Rc<String>),
41+
/// Expression expected to be constant but wasn't.
2542
NonConstantExpression,
43+
/// Expression expected to be an integer but wasn't.
2644
NonIntegerExpression,
45+
/// Attempted to assign to a `const` variable.
2746
CannotAssignToConst(Rc<String>),
47+
/// Duplicate `case` value in a `switch` statement.
2848
DuplicateCase,
49+
/// Invalid preprocessor directive.
2950
InvalidDirective(Rc<String>),
51+
/// String literal was not properly terminated.
3052
UnterminatedString,
53+
/// Invalid character.
3154
InvalidCharacter(char),
55+
/// Invalid escape sequence in a string literal.
3256
InvalidEscapeSequence,
57+
/// Expected a string literal but the given token was found instead (or `None` on EOF).
3358
ExpectedString(Option<TokenKind>),
59+
/// Error emitted by the [`wat`](https://crates.io/crates/wat) crate when compiling the WAT source to binary.
60+
///
61+
/// Note: Likely indicates a bug in this compiler!
3462
AssemblerError(String),
3563
}
3664

65+
/// Severity level of an error.
3766
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
3867
pub enum Severity {
68+
/// A warning that does not halt compilation.
69+
///
70+
/// Note: currently, no warnings are emitted.
3971
Warning,
72+
/// An error that halts compilation.
4073
Error,
4174
}
4275

76+
/// Compilation error or warning, including its location and context.
4377
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
4478
pub struct Error {
79+
/// The line number in the C source where the error occurred.
4580
pub(crate) line_number: usize,
81+
/// The column number in the C source where the error occurred.
4682
pub(crate) column: usize,
83+
/// The kind of error encountered.
4784
pub(crate) kind: Kind,
85+
/// The severity of the error.
4886
pub(crate) severity: Severity,
4987
}
5088

5189
impl Error {
90+
/// Returns the line number in the C source where the error occurred.
5291
pub fn line_number(&self) -> usize {
5392
self.line_number
5493
}
5594

95+
/// Returns the column number in the C source where the error occurred.
5696
pub fn column(&self) -> usize {
5797
self.column
5898
}
5999

100+
/// Returns the error kind.
60101
pub fn kind(&self) -> &Kind {
61102
&self.kind
62103
}
63104

105+
/// Returns the error severity level.
64106
pub fn severity(&self) -> Severity {
65107
self.severity
66108
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// SPDX-License-Identifier: MPL-2.0
33

44
#![no_std]
5+
#![warn(missing_docs)]
6+
#![doc = include_str!("../README.md")]
57

68
#[macro_use]
79
extern crate alloc;

0 commit comments

Comments
 (0)