Skip to content

Commit

Permalink
Merge pull request #61 from amber-lang/feat-static-lexer
Browse files Browse the repository at this point in the history
FEAT - Created static Lexer and feature flags
  • Loading branch information
Ph0enixKM authored Sep 10, 2024
2 parents e4b9f35 + bf5c5cd commit 4e55628
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 274 deletions.
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

0 comments on commit 4e55628

Please sign in to comment.