Browse Source

documentation updates

pull/1/head
Jason Williams 6 years ago
parent
commit
3141c7b2a2
  1. 2
      Cargo.lock
  2. 2
      Cargo.toml
  3. 24
      src/lib/syntax/ast/pos.rs
  4. 22
      src/lib/syntax/lexer.rs
  5. 2
      src/lib/syntax/mod.rs

2
Cargo.lock generated

@ -1,4 +1,4 @@
[[package]] [[package]]
name = "Boa" name = "boa"
version = "0.1.1" version = "0.1.1"

2
Cargo.toml

@ -1,5 +1,5 @@
[package] [package]
name = "Boa" name = "boa"
version = "0.1.1" version = "0.1.1"
authors = ["Jason Williams <jase.williams@gmail.com>"] authors = ["Jason Williams <jase.williams@gmail.com>"]
description = "Boa is a Javascript lexer, parser and Just-in-Time compiler written in Rust. Currently, it has support for some of the language." description = "Boa is a Javascript lexer, parser and Just-in-Time compiler written in Rust. Currently, it has support for some of the language."

24
src/lib/syntax/ast/pos.rs

@ -1,5 +1,9 @@
#[derive(Clone, PartialEq, Debug)] #[derive(Clone, PartialEq, Debug)]
// A position in Javascript source code /// A position in the Javascript source code
/// Stores both the column number and the line number
///
/// ## Similar Implementations
/// [V8: Location](https://cs.chromium.org/chromium/src/v8/src/parsing/scanner.h?type=cs&q=isValid+Location&g=0&l=216)
pub struct Position { pub struct Position {
// Column number // Column number
pub column_number: u64, pub column_number: u64,
@ -8,7 +12,23 @@ pub struct Position {
} }
impl Position { impl Position {
// Create a new position /// Create a new position, positions are usually created by Tokens..
///
/// # Arguments
///
/// * `line_number` - The line number the token starts at
/// * `column_number` - The column number the token starts at
///
/// # Example from Token
///
/// ```
/// pub fn new(data: TokenData, line_number: u64, column_number: u64) -> Token {
/// Token {
/// data: data,
/// pos: Position::new(line_number, column_number),
/// }
/// }
/// ```
pub fn new(line_number: u64, column_number: u64) -> Position { pub fn new(line_number: u64, column_number: u64) -> Position {
Position { Position {
line_number: line_number, line_number: line_number,

22
src/lib/syntax/lexer.rs

@ -1,3 +1,7 @@
//! A lexical analyzer for JavaScript source code.
//!
//! The Lexer splits its input source code into a sequence of input elements called tokens, represented by the [Token](../ast/token/struct.Token.html) structure.
//! It also removes whitespace and comments and attaches them to the next token.
use std::char::from_u32; use std::char::from_u32;
use std::error; use std::error;
use std::fmt; use std::fmt;
@ -54,7 +58,7 @@ macro_rules! op {
}); });
} }
// Defining an error type /// An error that occurred during lexing or compiling of the source input.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct LexerError { pub struct LexerError {
details: String, details: String,
@ -74,7 +78,6 @@ impl fmt::Display for LexerError {
} }
} }
// This is important for other errors to wrap this one.
impl error::Error for LexerError { impl error::Error for LexerError {
fn description(&self) -> &str { fn description(&self) -> &str {
&self.details &self.details
@ -86,7 +89,7 @@ impl error::Error for LexerError {
} }
} }
/// A javascript Lexer /// A lexical analyzer for JavaScript source code
pub struct Lexer<'a> { pub struct Lexer<'a> {
// The list fo tokens generated so far // The list fo tokens generated so far
pub tokens: Vec<Token>, pub tokens: Vec<Token>,
@ -99,6 +102,19 @@ pub struct Lexer<'a> {
} }
impl<'a> Lexer<'a> { impl<'a> Lexer<'a> {
/// Returns a Lexer with a buffer inside
///
/// # Arguments
///
/// * `buffer` - A string slice that holds the source code.
/// The buffer needs to have a lifetime as long as the Lexer instance itself
///
/// # Example
///
/// ```
/// let buffer = read_to_string("yourSourceCode.js").unwrap();
/// let lexer = Lexer::new(&buffer);
/// ```
pub fn new(buffer: &'a str) -> Lexer<'a> { pub fn new(buffer: &'a str) -> Lexer<'a> {
Lexer { Lexer {
tokens: Vec::new(), tokens: Vec::new(),

2
src/lib/syntax/mod.rs

@ -1,6 +1,6 @@
/// The Javascript Abstract Syntax Tree /// The Javascript Abstract Syntax Tree
pub mod ast; pub mod ast;
/// Parses a string stream into a sequence of tokens /// Lexical analysis (tokenizing/lexing).
pub mod lexer; pub mod lexer;
// Parses a sequence of tokens into expressions // Parses a sequence of tokens into expressions
pub mod parser; pub mod parser;

Loading…
Cancel
Save