Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT - Created static Lexer and feature flags #61

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions src/compiling/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use capitalize::Capitalize;
use std::fs::File;
use std::io::prelude::*;
use crate::compiling_rules::Rules;
use crate::compiling::{Token, Lexer, LexerError, LexerErrorType, Metadata, SyntaxModule};
use crate::compiling::{Token, LexerError, LexerErrorType, Metadata, SyntaxModule};
use crate::compiling::failing::message::Message;
use crate::compiling::failing::failure::Failure;
use crate::error_pos;

use super::lexer::Lexer;

/// How do you want to separate expressions?
///
Expand Down Expand Up @@ -64,37 +65,36 @@ pub enum ScopingMode {
pub struct Compiler {
/// Name of your language
pub name: String,
/// Rules that describe your language
pub rules: Rules,
/// Source code in a form of string
pub code: Option<String>,
/// Path to the compiled file if exists
pub path: Option<String>,
/// Separator mode for this compiler
pub separator_mode: SeparatorMode,
/// Scoping mode for this compiler
pub scoping_mode: ScopingMode,
// Check if user wants to debug parser
debug: bool
debug: bool,
/// Lexer to tokenize the code
lexer: Lexer
}

impl Compiler {
/// Create a new compiler with provided rules of your language
pub fn new<T: AsRef<str>>(name: T, rules: Rules) -> Self {
Compiler {
name: String::from(name.as_ref()),
rules,
code: None,
path: None,
separator_mode: SeparatorMode::Manual,
scoping_mode: ScopingMode::Block,
debug: false
debug: false,
lexer: Lexer::new(rules)
}
}

/// Set the language to use indentations
pub fn use_indents(&mut self) {
self.scoping_mode = ScopingMode::Indent
self.lexer.scoping_mode = ScopingMode::Indent
}

/// Set the language separator mode
pub fn set_separator(&mut self, mode: SeparatorMode) {
self.lexer.separator_mode = mode
}

/// Load file from path
Expand All @@ -119,9 +119,7 @@ impl Compiler {

/// Run just lexer
pub fn tokenize(&self) -> Result<Vec<Token>, LexerError> {
let mut lexer = Lexer::new(self);
lexer.run()?;
Ok(lexer.lexem)
self.lexer.tokenize(&self.code.clone().unwrap())
}

/// Parser will display information about the call stack
Expand Down
Loading
Loading