Skip to content

Commit

Permalink
Update lab ...
Browse files Browse the repository at this point in the history
Unified local worker and websocket lab host interfaces.

Connected port setting to code that creates websocket connections.

Connected controls to parser, and reestablished parsing

Added CST system from previous iteration

Various refactors and tweaks
  • Loading branch information
acweathersby committed Oct 4, 2024
1 parent 8e83b16 commit 8709094
Show file tree
Hide file tree
Showing 32 changed files with 1,118 additions and 457 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/radlr-core/test/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
RadlrResult,
TestPackage,
};
use std::{path::PathBuf, str::FromStr};
use std::{num::NonZero, path::PathBuf, str::FromStr};

/// Simple single thread compilation of a grammar source string.
/// `test_fn` is called after a successful compilation of parse states.
Expand Down
2 changes: 1 addition & 1 deletion crates/radlr-core/types/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ fn debug_items<'db, T: IntoIterator<Item = Item>>(comment: &str, items: T, db: &
eprintln!("\n {} --> ", comment);

for item in items {
eprintln!(" {}", item._debug_string_w_db_(db));
eprintln!("--- {}", item._debug_string_w_db_(db));
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/radlr-core/types/parse_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use radlr_rust_runtime::types::bytecode::MatchInputType;

use super::*;
use crate::{
compile::states::build_graph::graph::ScannerData,
compile::states::build_graph::graph::{create_scanner_name, ScannerData},
parser::{self, ASTNode, State},
writer::code_writer::CodeWriter,
};
Expand Down Expand Up @@ -152,7 +152,7 @@ impl ParseState {
self.guid_name.to_string(db.string_store()),
&self.code.split("\n").collect::<Vec<_>>().join("\n "),
&self.get_ast(),
self.scanner.as_ref().map(|scanner| { scanner.create_scanner_name(db) })
self.scanner.as_ref().map(|scanner| { create_scanner_name(db, scanner.hash) })
)
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/radlr-core/types/parser_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ impl ParserConfig {

/// Used to track the type of parser that has been created by radlr.
#[derive(Default, Clone, Copy, Debug)]
#[repr(C)]
pub struct ParserClassification {
/// Maximum peek level used to disambiguate conflicting phrases. If this is
/// equal to `u16::MAX`, then peeking failed or a fork was used in its place.
Expand Down
9 changes: 7 additions & 2 deletions crates/radlr-lab/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ tungstenite = { version="0.24.0", optional = true }

[features]
host = ["tungstenite"]
client = []
client = ["wasm-bindgen"]

[profile.release]
opt-level=3
panic = 'abort'
lto = true
debug = 0
debug = 0


[dependencies.wasm-bindgen]
optional = true
version = "0.2.87"
105 changes: 94 additions & 11 deletions crates/radlr-lab/lib.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,50 @@
//!
//! # RADLR LAB
pub mod serialize;

use radlr_core::{ParserConfig, ParserStore, RadlrDatabase, RadlrError, RadlrGrammar, RadlrIRParser, RadlrResult};
use radlr_bytecode::compile_bytecode;
use radlr_core::{
ParserClassification,
ParserConfig,
ParserStore,
RadlrDatabase,
RadlrError,
RadlrGrammar,
RadlrIRParser,
RadlrResult,
};

use std::io;
#[cfg(feature = "host")]
use std::path::PathBuf;

pub mod serialize;
#[cfg(feature = "host")]
use tungstenite::*;

#[cfg(feature = "host")]
use std::{net::TcpListener, thread::spawn};

#[cfg(feature = "client")]
use wasm_bindgen::prelude::*;

#[repr(u8)]
enum WSRequestCodes {
#[cfg_attr(feature = "client", wasm_bindgen)]
pub enum WSRequestCodes {
Undefined = 0,
/**
* Request to build grammar artifacts (parser, states, ast, etc)
*/
BuildGrammar = 1,
Ping = 2,
}

#[repr(u8)]
enum WSResponseCodes {
Undefined = 0,
#[cfg_attr(feature = "client", wasm_bindgen)]
pub enum WSResponseCodes {
Undefined = 0,
Classification = 1,
ByteCode = 2,
Pong = 3,
}

const DEFAULT_PORT: u16 = 15421;
Expand All @@ -27,9 +54,6 @@ pub fn run_lab_server(port: Option<u16>) -> Result<(), RadlrError> {
let port = port.unwrap_or(DEFAULT_PORT);
let ip4 = "0.0.0.0";

use std::{net::TcpListener, thread::spawn};
use tungstenite::*;

let server = TcpListener::bind(&format!("{ip4}:{port}"))?;

println!("Listening on {ip4}:{port}");
Expand Down Expand Up @@ -66,10 +90,21 @@ pub fn run_lab_server(port: Option<u16>) -> Result<(), RadlrError> {
println!("Text received {text_data:?}");
}
Message::Binary(binary_data) => {
if binary_data.len() == 1 {
if binary_data[0] == WSRequestCodes::Ping as u8 {
websocket.send(Message::Binary(vec![WSResponseCodes::Pong as u8])).unwrap();
websocket.flush().unwrap();
websocket.send(Message::Close(None)).unwrap();
} else {
websocket.send(Message::Close(None)).unwrap();
}
}

if binary_data.len() == 0 {
println!("Ignoring empty binary");
continue;
}

match unsafe { std::mem::transmute::<_, WSRequestCodes>(binary_data[0]) } {
WSRequestCodes::BuildGrammar => {
println!("Request to build grammar");
Expand Down Expand Up @@ -137,9 +172,20 @@ pub fn run_lab_server(port: Option<u16>) -> Result<(), RadlrError> {
};

println!("Built parser: Type {:?}", parser.get_classification().to_string());
println!("Sending data back!");

let classification = parser.get_classification();
if let Err(err) = host_send_classification(classification, &mut websocket) {
eprintln!("Error encountered sending parser classification data\n {err}");
}

if let Err(err) = host_send_bytecode_db(parser, &mut websocket) {
eprintln!("Error encountered sending bytecode data\n {err}");
};

//Ok(parser)
continue;
}

Err(err) => {
eprintln!("Errors encountered in grammar {err}");
continue;
Expand Down Expand Up @@ -178,5 +224,42 @@ pub fn run_lab_server(port: Option<u16>) -> Result<(), RadlrError> {
Ok(())
}

// - Get Parser States
// - Get
#[cfg(feature = "host")]

fn host_send_bytecode_db(
parser: RadlrIRParser,
websocket: &mut WebSocket<std::net::TcpStream>,
) -> Result<(), tungstenite::Error> {
let bytecode = match compile_bytecode(&parser, true) {
Err(err) => return Err(tungstenite::Error::Io(io::Error::new(io::ErrorKind::Other, err.to_string()))),
Ok(data) => data,
};

let mut data = serialize::bytecode_db::export_bytecode_db(&bytecode);
let mut export_data = Vec::new();
export_data.push(WSResponseCodes::ByteCode as u8);
export_data.append(&mut data);
websocket.write(Message::Binary(export_data))?;
websocket.flush()?;
Ok(())
}

#[cfg(feature = "host")]
/// Emit a message with containing a parser classification structure
fn host_send_classification(
classification: radlr_core::ParserClassification,
websocket: &mut tungstenite::WebSocket<std::net::TcpStream>,
) -> Result<(), tungstenite::Error> {
let mut data = [0u8; 1 + size_of::<ParserClassification>()];

data[0] = WSResponseCodes::Classification as u8;

unsafe {
std::ptr::copy(std::mem::transmute(&classification), data.as_mut_ptr().offset(1), size_of::<ParserClassification>());
};

websocket.write(Message::Binary(data.to_vec()))?;
websocket.flush()?;

Ok(())
}
16 changes: 8 additions & 8 deletions crates/radlr-test/bytecode/test_parsing.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::utils::{
compile_and_run_grammars,
map_reduce_function,
TestParser,
_write_disassembly_to_temp_file_,
_write_states_to_temp_file_,
};
compile_and_run_grammars,
map_reduce_function,
TestParser,
_write_disassembly_to_temp_file_,
_write_states_to_temp_file_,
};
use radlr_bytecode::compile_bytecode;
use radlr_core::{test::utils::build_parse_states_from_source_str, *};
use radlr_rust_runtime::types::{ASTConstructor, AstSlotNew, EntryPoint, ParserInitializer, StringInput};
Expand All @@ -27,7 +27,6 @@ pub fn group_inline() -> RadlrResult<()> {
)
}


#[test]
pub fn should_fail_on_infinity_recursive_non_terminal() -> RadlrResult<()> {
compile_and_run_grammars(
Expand All @@ -37,7 +36,8 @@ pub fn should_fail_on_infinity_recursive_non_terminal() -> RadlrResult<()> {
"#],
&[("default", "", false)],
ParserConfig::default(),
).expect_err("Should have thrown an error");
)
.expect_err("Should have thrown an error");

Ok(())
}
Expand Down
4 changes: 3 additions & 1 deletion crates/radlr-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ path="../radlr-rust-runtime"
path="../radlr-bytecode"

[dependencies.radlr_lab]
path="../radlr-lab"
path="../radlr-lab"
features=["client"]


[dev-dependencies]
wasm-bindgen-test = "0.3.37"
Expand Down
37 changes: 18 additions & 19 deletions crates/radlr-wasm/error.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use std::default;

use radlr_core::RadlrError;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
#[derive(Clone, Copy, Default)]
pub enum ErrorOrigin {
#[default]
Grammar,
ParserBuild,
Grammar,
ParserBuild,
StatesCreation,
Ir,
BytecodeImport
BytecodeImport,
}

#[wasm_bindgen]
Expand Down Expand Up @@ -60,7 +58,7 @@ impl From<(&Vec<RadlrError>, ErrorOrigin)> for PositionedErrors {
}
}

impl From<(Vec<RadlrError>, ErrorOrigin)> for PositionedErrors {
impl From<(Vec<RadlrError>, ErrorOrigin)> for PositionedErrors {
fn from((mut errors, origin): (Vec<RadlrError>, ErrorOrigin)) -> Self {
let mut out = PositionedErrors { vec: vec![] };
out.extend(&mut errors, origin);
Expand All @@ -69,15 +67,15 @@ impl From<(Vec<RadlrError>, ErrorOrigin)> for PositionedErrors {
}

impl From<(RadlrError, ErrorOrigin)> for PositionedErrors {
fn from( (err, origin): (RadlrError, ErrorOrigin)) -> Self {
fn from((err, origin): (RadlrError, ErrorOrigin)) -> Self {
PositionedErrors { vec: convert_error(&err, origin) }
}
}

impl From<(&Vec<&RadlrError>, ErrorOrigin)> for PositionedErrors {
fn from((errors, origin): (&Vec<&RadlrError>, ErrorOrigin)) -> Self {
let mut out = PositionedErrors { vec: vec![] };
out.extend_from_refs(&errors.into_iter().map(|e| (*e).clone()).collect(), origin);
out.extend_from_refs(&errors.into_iter().map(|e| (*e).clone()).collect(), origin);
out
}
}
Expand All @@ -99,26 +97,27 @@ fn convert_error(err: &RadlrError, origin: ErrorOrigin) -> Vec<JSRadlrSourceErro
.map(|(loc, _, msg)| {
let range = loc.get_range();
JSRadlrSourceError {
col: range.start_column,
line: range.start_line,
len: loc.len() as u32,
col: range.start_column,
line: range.start_line,
len: loc.len() as u32,
start_offset: loc.get_start() as u32,
end_offset: loc.get_end() as u32,
message: base_message.clone() + ":\n " + msg,origin
end_offset: loc.get_end() as u32,
message: base_message.clone() + ":\n " + msg,
origin,
}
})
.collect(),

RadlrError::SourceError { loc, msg, .. } => {
let range = loc.get_range();
vec![JSRadlrSourceError {
col: range.start_column,
line: range.start_line,
len: loc.len() as u32,
col: range.start_column,
line: range.start_line,
len: loc.len() as u32,
start_offset: loc.get_start() as u32,
end_offset: loc.get_end() as u32,
message: msg.clone(),
origin
end_offset: loc.get_end() as u32,
message: msg.clone(),
origin,
}]
}
RadlrError::StaticText(text) => {
Expand Down
19 changes: 19 additions & 0 deletions crates/radlr-wasm/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,25 @@ pub fn get_debug_state_name(address: u32, pkg: &JSBytecodeParserDB, db: &JSParse
}
}

#[wasm_bindgen]
pub fn is_instruction_transitory(address: u32, pkg: &JSBytecodeParserDB) -> bool {
let i = Instruction::from((pkg.0.bytecode.as_slice(), address as usize));
let opcode = i.get_opcode();
match opcode {
Opcode::ShiftToken
| Opcode::ShiftChar
| Opcode::ShiftTokenScanless
| Opcode::SkipToken
| Opcode::SkipTokenScanless
| Opcode::PeekReset
| Opcode::PeekSkipToken
| Opcode::PeekTokenScanless
| Opcode::PeekSkipTokenScanless
| Opcode::PeekToken => true,
_ => false,
}
}

#[wasm_bindgen]
#[derive(Default)]
pub struct TokenOffsets {
Expand Down
1 change: 1 addition & 0 deletions crates/radlr-wasm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod lsp;
pub mod parser;
mod types;

pub use radlr_lab::*;
pub use types::*;

pub use parser::{JSDebugEvent, JSDebugPacket};
Loading

0 comments on commit 8709094

Please sign in to comment.