Skip to content

Commit

Permalink
move ident file
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Hansen committed Apr 14, 2021
1 parent 4acfb83 commit 24dd5ca
Show file tree
Hide file tree
Showing 19 changed files with 85 additions and 83 deletions.
4 changes: 2 additions & 2 deletions parser/src/control_flow_analysis/flow_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//! execution.
use crate::{
parse_tree::{Ident, Visibility},
parse_tree::Visibility,
semantics::ast_node::{TypedEnumVariant, TypedExpressionVariant, TypedTraitDeclaration},
TreeType,
Ident, TreeType,
};
use crate::{
semantics::{
Expand Down
51 changes: 51 additions & 0 deletions parser/src/ident.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::error::*;
use crate::parser::Rule;
use pest::iterators::Pair;
use pest::Span;
use std::hash::{Hash, Hasher};
#[derive(Debug, Clone)]
pub struct Ident<'sc> {
pub(crate) primary_name: &'sc str,
// sub-names are the stuff after periods
// like x.test.thing.method()
// `test`, `thing`, and `method` are sub-names
// the primary name is `x`
pub(crate) span: Span<'sc>,
}

// custom implementation of Hash so that namespacing isn't reliant on the span itself, which will
// always be different.
impl Hash for Ident<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.primary_name.hash(state);
}
}
impl PartialEq for Ident<'_> {
fn eq(&self, other: &Self) -> bool {
self.primary_name == other.primary_name
}
}

impl Eq for Ident<'_> {}

impl<'sc> Ident<'sc> {
pub(crate) fn parse_from_pair(pair: Pair<'sc, Rule>) -> CompileResult<'sc, Ident> {
let span = {
let pair = pair.clone();
if pair.as_rule() != Rule::ident {
pair.into_inner().next().unwrap().as_span()
} else {
pair.as_span()
}
};
let name = pair.as_str().trim();
ok(
Ident {
primary_name: name,
span,
},
Vec::new(),
Vec::new(),
)
}
}
15 changes: 9 additions & 6 deletions parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,28 @@ extern crate pest_derive;
mod error;

mod control_flow_analysis;
mod ident;
mod parse_tree;
mod parser;
mod semantics;
pub(crate) mod types;
pub(crate) mod utils;

use crate::error::*;
pub use crate::parse_tree::Ident;
use crate::parse_tree::*;
pub(crate) use crate::parse_tree::{Expression, UseStatement, WhileLoop};
use crate::parser::{HllParser, Rule};
use control_flow_analysis::ControlFlowGraph;
use pest::iterators::Pair;
use pest::Parser;
pub use semantics::{Namespace, TypedDeclaration, TypedFunctionDeclaration};
use semantics::{TreeType, TypedParseTree};
pub use types::TypeInfo;

pub(crate) mod types;
pub(crate) mod utils;
pub(crate) use crate::parse_tree::{Expression, UseStatement, WhileLoop};

pub use error::{CompileError, CompileResult, CompileWarning};
pub use ident::Ident;
pub use pest::Span;
pub use semantics::{Namespace, TypedDeclaration, TypedFunctionDeclaration};
pub use types::TypeInfo;

// todo rename to language name
#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion parser/src/parse_tree/call_path.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::Ident;
use crate::error::*;
use crate::parser::Rule;
use crate::Ident;
use pest::iterators::Pair;
use pest::Span;

Expand Down
4 changes: 2 additions & 2 deletions parser/src/parse_tree/declaration/function_declaration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::error::*;
use crate::parse_tree::{declaration::TypeParameter, Ident};
use crate::parse_tree::declaration::TypeParameter;
use crate::types::TypeInfo;
use crate::{CodeBlock, Rule};
use crate::{CodeBlock, Ident, Rule};
use inflector::cases::snakecase::is_snake_case;
use pest::iterators::Pair;
use pest::Span;
Expand Down
3 changes: 2 additions & 1 deletion parser/src/parse_tree/declaration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ pub(crate) use type_parameter::*;
pub(crate) use variable_declaration::*;

use crate::error::*;
use crate::parse_tree::{Expression, Ident};
use crate::parse_tree::Expression;
use crate::parser::Rule;
use crate::types::TypeInfo;
use crate::Ident;
use pest::iterators::Pair;

#[derive(Debug, Clone)]
Expand Down
3 changes: 2 additions & 1 deletion parser/src/parse_tree/declaration/reassignment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::error::{err, ok, CompileResult};
use crate::parse_tree::{Expression, Ident};
use crate::parse_tree::Expression;
use crate::parser::Rule;
use crate::Ident;
use pest::iterators::Pair;
use pest::Span;

Expand Down
4 changes: 2 additions & 2 deletions parser/src/parse_tree/declaration/struct_declaration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::error::*;
use crate::parse_tree::{declaration::TypeParameter, Ident};
use crate::parse_tree::declaration::TypeParameter;
use crate::parser::Rule;
use crate::types::TypeInfo;
use crate::{error::*, Ident};
use inflector::cases::classcase::is_class_case;
use inflector::cases::snakecase::is_snake_case;
use pest::iterators::Pair;
Expand Down
4 changes: 2 additions & 2 deletions parser/src/parse_tree/declaration/trait_declaration.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::{FunctionDeclaration, FunctionParameter, Visibility};
use crate::error::*;
use crate::parse_tree::{Ident, TypeParameter};
use crate::parse_tree::TypeParameter;
use crate::parser::Rule;
use crate::types::TypeInfo;
use crate::{error::*, Ident};
use inflector::cases::classcase::is_class_case;
use inflector::cases::snakecase::is_snake_case;
use pest::iterators::Pair;
Expand Down
4 changes: 2 additions & 2 deletions parser/src/parse_tree/declaration/variable_declaration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::parse_tree::{Expression, Ident};
use crate::types::TypeInfo;
use crate::parse_tree::Expression;
use crate::{types::TypeInfo, Ident};

#[derive(Debug, Clone)]
pub(crate) struct VariableDeclaration<'sc> {
Expand Down
2 changes: 1 addition & 1 deletion parser/src/parse_tree/expression/asm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::error::*;
use crate::parse_tree::Ident;
use crate::parser::Rule;
use crate::Ident;
use pest::iterators::Pair;
use pest::Span;

Expand Down
57 changes: 3 additions & 54 deletions parser/src/parse_tree/expression/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use crate::error::*;
use crate::parse_tree::CallPath;
use crate::parse_tree::Literal;
use crate::parse_tree::{CallPath, Literal};
use crate::parser::Rule;
use crate::CodeBlock;
use crate::{CodeBlock, Ident};
use either::Either;
use pest::iterators::Pair;
use pest::Span;
use std::{
collections::HashMap,
hash::{Hash, Hasher},
};
use std::collections::HashMap;

mod asm;
pub(crate) use asm::AsmExpression;
Expand Down Expand Up @@ -724,53 +720,6 @@ impl UnaryOp {
}
}

#[derive(Debug, Clone)]
pub struct Ident<'sc> {
pub(crate) primary_name: &'sc str,
// sub-names are the stuff after periods
// like x.test.thing.method()
// `test`, `thing`, and `method` are sub-names
// the primary name is `x`
pub(crate) span: Span<'sc>,
}

// custom implementation of Hash so that namespacing isn't reliant on the span itself, which will
// always be different.
impl Hash for Ident<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.primary_name.hash(state);
}
}
impl PartialEq for Ident<'_> {
fn eq(&self, other: &Self) -> bool {
self.primary_name == other.primary_name
}
}

impl Eq for Ident<'_> {}

impl<'sc> Ident<'sc> {
pub(crate) fn parse_from_pair(pair: Pair<'sc, Rule>) -> CompileResult<'sc, Ident> {
let span = {
let pair = pair.clone();
if pair.as_rule() != Rule::ident {
pair.into_inner().next().unwrap().as_span()
} else {
pair.as_span()
}
};
let name = pair.as_str().trim();
ok(
Ident {
primary_name: name,
span,
},
Vec::new(),
Vec::new(),
)
}
}

fn parse_op<'sc>(op: Pair<'sc, Rule>) -> CompileResult<'sc, Op> {
use OpVariant::*;
let mut errors = Vec::new();
Expand Down
2 changes: 0 additions & 2 deletions parser/src/parse_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ mod declaration;
mod expression;
mod literal;
mod use_statement;
mod variable_declaration;
mod while_loop;

pub(crate) use call_path::*;
pub(crate) use code_block::*;
pub(crate) use declaration::*;
pub use expression::Ident;
pub(crate) use expression::{AsmExpression, Expression, UnaryOp};
pub(crate) use literal::Literal;
pub(crate) use use_statement::{ImportType, UseStatement};
Expand Down
1 change: 0 additions & 1 deletion parser/src/parse_tree/variable_declaration.rs

This file was deleted.

2 changes: 1 addition & 1 deletion parser/src/semantics/ast_node/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::{
};
use crate::parse_tree::*;
use crate::semantics::Namespace;
use crate::{error::*, types::ResolvedType};
use crate::{error::*, types::ResolvedType, Ident};
use pest::Span;

#[derive(Clone, Debug)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use crate::parse_tree::Ident;
use crate::semantics::ast_node::*;
use crate::Ident;
#[derive(Clone, Debug)]
pub(crate) enum TypedExpressionVariant<'sc> {
Literal(Literal<'sc>),
Expand Down
4 changes: 2 additions & 2 deletions parser/src/semantics/ast_node/impl_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use super::{
declaration::{TypedFunctionParameter, TypedTraitFn},
ERROR_RECOVERY_DECLARATION,
};
use crate::parse_tree::{Ident, ImplTrait};
use crate::parse_tree::ImplTrait;
use crate::semantics::{Namespace, TypedDeclaration, TypedFunctionDeclaration};
use crate::{error::*, types::ResolvedType};
use crate::{Ident, error::*, types::ResolvedType};

pub(crate) fn implementation_of_trait<'sc>(
impl_trait: ImplTrait<'sc>,
Expand Down
2 changes: 1 addition & 1 deletion parser/src/semantics/ast_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::error::*;
use crate::parse_tree::*;
use crate::semantics::Namespace;
use crate::types::{ResolvedType, TypeInfo};
use crate::{AstNode, AstNodeContent, ReturnStatement};
use crate::{AstNode, AstNodeContent, ReturnStatement, Ident};
use declaration::TypedTraitFn;
use pest::Span;

Expand Down
2 changes: 1 addition & 1 deletion parser/src/types/type_info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::*;
use crate::{parse_tree::Ident, Rule};
use crate::{Ident, Rule};
use pest::iterators::Pair;

use super::ResolvedType;
Expand Down

0 comments on commit 24dd5ca

Please sign in to comment.