Skip to content

Commit

Permalink
update: parser code
Browse files Browse the repository at this point in the history
  • Loading branch information
apskhem committed Mar 22, 2023
1 parent 5a595b4 commit 97d43e4
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 153 deletions.
4 changes: 4 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::ops::Range;

use pest::Span;
use pest::iterators::Pair;
use crate::parser::Rule;

type SpanRange = Range<usize>;

pub mod enums;
pub mod indexes;
pub mod refs;
Expand Down
6 changes: 3 additions & 3 deletions src/ast/project.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{str::FromStr, ops::Range};
use std::str::FromStr;

use pest::Span;
use super::*;

#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub enum DatabaseType {
Expand All @@ -27,7 +27,7 @@ impl FromStr for DatabaseType {

#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct ProjectBlock {
pub span_range: Range<usize>,
pub span_range: SpanRange,
pub name: String,
pub database_type: DatabaseType,
pub note: Option<String>
Expand Down
2 changes: 1 addition & 1 deletion src/ast/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::*;

#[derive(Debug, PartialEq, Clone)]
pub struct SchemaBlock<'a> {
pub span_range: Range<usize>,
pub span_range: SpanRange,
/// Input source content.
pub input: &'a str,
/// Overall description of the project. This is optional. The file must contain one or zero 'Project' block.
Expand Down
27 changes: 20 additions & 7 deletions src/ast/table.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
use std::{ops::Range, str::FromStr, collections::HashMap};
use std::str::FromStr;

use super::*;

#[derive(Debug, PartialEq, Clone, Default)]
pub struct TableBlock {
pub span_range: Range<usize>,
pub span_range: SpanRange,
pub cols: Vec<TableColumn>,
pub ident: TableIdent,
pub note: Option<String>,
pub indexes: Option<indexes::IndexesBlock>,
pub settings: Option<HashMap<String, Value>>,
pub settings: Option<Vec<(String, Value)>>,
pub meta_indexer: TableIndexer
}

#[derive(Debug, PartialEq, Clone, Default)]
pub struct ColumnType {
pub span_range: Range<usize>,
pub span_range: SpanRange,
pub type_name: ColumnTypeName,
pub args: Vec<Value>,
pub arrays: Vec<Option<usize>>,
}

#[derive(Debug, PartialEq, Clone, Default)]
pub struct TableColumn {
pub span_range: Range<usize>,
pub span_range: SpanRange,
pub name: String,
pub r#type: ColumnType,
pub settings: ColumnSettings,
Expand All @@ -47,6 +47,19 @@ pub enum Value {
Null
}

impl FromStr for Value {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"true" => Ok(Value::Bool(true)),
"false" => Ok(Value::Bool(false)),
"null" => Ok(Value::Null),
_ => Err(()),
}
}
}

impl ToString for Value {
fn to_string(&self) -> String {
match self {
Expand Down Expand Up @@ -182,7 +195,7 @@ impl FromStr for ColumnTypeName {

#[derive(Debug, PartialEq, Clone, Default)]
pub struct ColumnSettings {
pub span_range: Range<usize>,
pub span_range: SpanRange,
pub is_pk: bool,
pub is_unique: bool,
pub is_nullable: bool,
Expand All @@ -194,7 +207,7 @@ pub struct ColumnSettings {

#[derive(Debug, PartialEq, Clone, Default)]
pub struct TableIdent {
pub span_range: Range<usize>,
pub span_range: SpanRange,
pub name: String,
pub schema: Option<String>,
pub alias: Option<String>
Expand Down
Loading

0 comments on commit 97d43e4

Please sign in to comment.