Skip to content

Commit

Permalink
Fix missing docs
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed Jan 28, 2024
1 parent e01700a commit 1f973b6
Show file tree
Hide file tree
Showing 10 changed files with 565 additions and 54 deletions.
4 changes: 4 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ pub(crate) fn sentinel(start: usize) -> Token {
}

impl Token {
/// Access token value
pub fn unwrap(self) -> String {
self.1.unwrap()
}
/// Take token value
pub fn take(&mut self) -> Self {
Token(self.0, self.1.take(), self.2)
}
Expand Down Expand Up @@ -64,6 +66,7 @@ fn from_bytes(bytes: &[u8]) -> String {
include!(concat!(env!("OUT_DIR"), "/keywords.rs"));
pub(crate) const MAX_KEYWORD_LEN: usize = 17;

/// Check if `word` is a keyword
pub fn keyword_token(word: &[u8]) -> Option<TokenType> {
KEYWORDS
.get(UncasedStr::new(unsafe { str::from_utf8_unchecked(word) }))
Expand Down Expand Up @@ -239,6 +242,7 @@ pub(crate) fn from_token(ty: u16, value: Token) -> String {
}

impl TokenType {
/// Return the associated string (mainly for testing)
pub const fn as_str(&self) -> Option<&'static str> {
use TokenType::*;
match self {
Expand Down
2 changes: 1 addition & 1 deletion src/dialect/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Renamed manually.
// To be keep in sync.
#[non_exhaustive]
#[allow(non_camel_case_types)]
#[allow(non_camel_case_types, missing_docs)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd)]
#[repr(u16)]
pub enum TokenType {
Expand Down
10 changes: 8 additions & 2 deletions src/lexer/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use std::error::Error;
use std::fmt;
use std::io;

/// Error with position
pub trait ScanError: Error + From<io::Error> + Sized {
/// Update the position where the error occurs
fn position(&mut self, line: u64, column: usize);
}

Expand All @@ -17,8 +19,10 @@ type SplitResult<'input, TokenType, Error> =

/// Split function used to tokenize the input
pub trait Splitter: Sized {
/// Potential error raised
type Error: ScanError;
//type Item: ?Sized;
/// Token generated
type TokenType;

/// The arguments are an initial substring of the remaining unprocessed
Expand Down Expand Up @@ -55,6 +59,7 @@ pub struct Scanner<S: Splitter> {
}

impl<S: Splitter> Scanner<S> {
/// Constructor
pub fn new(splitter: S) -> Scanner<S> {
Scanner {
offset: 0,
Expand All @@ -74,14 +79,15 @@ impl<S: Splitter> Scanner<S> {
pub fn column(&self) -> usize {
self.column
}

/// Associated splitter
pub fn splitter(&self) -> &S {
&self.splitter
}

/// Mark current position
pub fn mark(&mut self) {
self.mark = (self.offset, self.line, self.column);
}
/// Reset to mark
pub fn reset_to_mark(&mut self) {
(self.offset, self.line, self.column) = self.mark;
}
Expand Down
11 changes: 11 additions & 0 deletions src/lexer/sql/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,31 @@ use std::io;
use crate::lexer::scan::ScanError;
use crate::parser::ParserError;

/// SQL lexer and parser errors
#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
/// I/O Error
Io(io::Error),
/// Lexer error
UnrecognizedToken(Option<(u64, usize)>),
/// Missing quote or double-quote or backtick
UnterminatedLiteral(Option<(u64, usize)>),
/// Missing `]`
UnterminatedBracket(Option<(u64, usize)>),
/// Missing `*/`
UnterminatedBlockComment(Option<(u64, usize)>),
/// Invalid parameter name
BadVariableName(Option<(u64, usize)>),
/// Invalid number format
BadNumber(Option<(u64, usize)>),
/// Invalid or missing sign after `!`
ExpectedEqualsSign(Option<(u64, usize)>),
/// BLOB literals are string literals containing hexadecimal data and preceded by a single "x" or "X" character.
MalformedBlobLiteral(Option<(u64, usize)>),
/// Hexadecimal integer literals follow the C-language notation of "0x" or "0X" followed by hexadecimal digits.
MalformedHexInteger(Option<(u64, usize)>),
/// Grammar error
ParserError(ParserError, Option<(u64, usize)>),
}

Expand Down
10 changes: 8 additions & 2 deletions src/lexer/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ pub use error::Error;
// TODO Extract scanning stuff and move this into the parser crate
// to make possible to use the tokenizer without depending on the parser...

/// SQL parser
pub struct Parser<'input> {
input: &'input [u8],
scanner: Scanner<Tokenizer>,
parser: yyParser<'input>,
}

impl<'input> Parser<'input> {
/// Constructor
pub fn new(input: &'input [u8]) -> Parser<'input> {
let lexer = Tokenizer::new();
let scanner = Scanner::new(lexer);
Expand All @@ -42,15 +44,16 @@ impl<'input> Parser<'input> {
parser,
}
}

/// Parse new `input`
pub fn reset(&mut self, input: &'input [u8]) {
self.input = input;
self.scanner.reset();
}

/// Current line position in input
pub fn line(&self) -> u64 {
self.scanner.line()
}
/// Current column position in input
pub fn column(&self) -> usize {
self.scanner.column()
}
Expand Down Expand Up @@ -241,12 +244,15 @@ impl<'input> FallibleIterator for Parser<'input> {
}
}

/// SQL token
pub type Token<'input> = (&'input [u8], TokenType);

/// SQL lexer
#[derive(Default)]
pub struct Tokenizer {}

impl Tokenizer {
/// Constructor
pub fn new() -> Tokenizer {
Tokenizer {}
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! SQLite3 syntax lexer and parser
#![warn(missing_docs)]

pub mod dialect;
// In Lemon, the tokenizer calls the parser.
pub mod lexer;
Expand Down
6 changes: 4 additions & 2 deletions src/parser/ast/check.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Check for additional syntax error
use crate::ast::*;
use crate::custom_err;
use std::fmt::{Display, Formatter};
Expand Down Expand Up @@ -271,11 +272,12 @@ impl OneSelect {
}
}
}

pub fn push(values: &mut [Vec<Expr>], v: Vec<Expr>) -> Result<(), ParserError> {
///
pub fn push(values: &mut Vec<Vec<Expr>>, v: Vec<Expr>) -> Result<(), ParserError> {
if values[0].len() != v.len() {
return Err(custom_err!("all VALUES must have the same number of terms"));
}
values.push(v);
Ok(())
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/parser/ast/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! AST node format
use std::fmt::{self, Display, Formatter, Write};

use crate::ast::*;
Expand Down Expand Up @@ -45,15 +46,19 @@ impl<'a, 'b> TokenStream for FmtTokenStream<'a, 'b> {
}
}

/// Stream of token
pub trait TokenStream {
/// Potential error raised
type Error;

/// Push token to this stream
fn append(&mut self, ty: TokenType, value: Option<&str>) -> Result<(), Self::Error>;
}

/// Generate token(s) from AST node
pub trait ToTokens {
/// Send token(s) to the specified stream
fn to_tokens<S: TokenStream>(&self, s: &mut S) -> Result<(), S::Error>;

/// Format AST node
fn to_fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut s = FmtTokenStream { f, spaced: true };
self.to_tokens(&mut s)
Expand Down
Loading

0 comments on commit 1f973b6

Please sign in to comment.