Skip to content

Commit

Permalink
support parser whole file
Browse files Browse the repository at this point in the history
  • Loading branch information
sbwtw committed Nov 14, 2024
1 parent 598eef2 commit c1cb1a8
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 12 deletions.
5 changes: 4 additions & 1 deletion lib/src/backend/lua/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ fn generate_module<S1: AsRef<str>, S2: AsRef<str>>(decl: S1, body: S2, writer: &
let mgr = UnitsManager::new();
let ctx = ModuleContext::new(ModuleKind::Application);
let mut lexer = StLexerBuilder::new().build_str(decl.as_ref());
let decl = ParserBuilder::default().build().parse(&mut lexer).unwrap();
let decl = ParserBuilder::default()
.build()
.parse_decl(&mut lexer)
.unwrap();
let fun_id = ctx.write().add_declaration(decl, Uuid::nil());

let mut lexer = StLexerBuilder::new().build_str(body.as_ref());
Expand Down
8 changes: 8 additions & 0 deletions lib/src/parser/default_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ impl DefaultParser {
}

impl ParserTrait for DefaultParser {
fn name(&self) -> String {
"DefaultParser".to_string()
}

fn parse_pou(&self, lexer: &mut StLexer) -> Result<(Declaration, Statement), ParseError> {
todo!()
}

#[inline]
fn parse_decl(&self, lexer: &mut StLexer) -> Result<Declaration, ParseError> {
DefaultParserImpl::new(lexer.into_iter()).parse_declaration()
Expand Down
18 changes: 16 additions & 2 deletions lib/src/parser/lalrpop_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ where

pub struct LalrpopParser {
decl_parser: Lazy<st::DeclarationParser>,
body_parser: Lazy<st::StFunctionParser>,
body_parser: Lazy<st::StBodyParser>,
pou_parser: Lazy<st::StPOUParser>,
literal_parser: Lazy<st::LiteralExprParser>,
expression_parser: Lazy<st::ExprParser>,
}
Expand All @@ -53,14 +54,27 @@ impl Default for LalrpopParser {
fn default() -> Self {
Self {
decl_parser: Lazy::new(st::DeclarationParser::new),
body_parser: Lazy::new(st::StFunctionParser::new),
body_parser: Lazy::new(st::StBodyParser::new),
pou_parser: Lazy::new(st::StPOUParser::new),
literal_parser: Lazy::new(st::LiteralExprParser::new),
expression_parser: Lazy::new(st::ExprParser::new),
}
}
}

impl ParserTrait for LalrpopParser {
#[inline]
fn name(&self) -> String {
"Lalrpop Parser".to_string()
}

#[inline]
fn parse_pou(&self, lexer: &mut StLexer) -> Result<(Declaration, Statement), ParseError> {
self.pou_parser
.parse(LalrPopLexerWrapper::new(lexer.into_iter()))
.map_err(Into::into)
}

#[inline]
fn parse_decl(&self, lexer: &mut StLexer) -> Result<Declaration, ParseError> {
self.decl_parser
Expand Down
9 changes: 7 additions & 2 deletions lib/src/parser/lalrpop_impl/st.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,13 @@ SmallComma3<T>: SmallVec3<T> = {
}
};

/// Top-level ST function
pub StFunction = StatementList;
/// Top-Level ST decl + impl body
pub StPOU: (Declaration, Statement) = {
Declaration StatementList => (<>),
}

/// Top-level ST function body
pub StBody = StatementList;

/// ST Statements
StatementList: Statement = {
Expand Down
14 changes: 13 additions & 1 deletion lib/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,17 @@ pub struct Parser<T: ParserTrait> {

impl<T: ParserTrait> Parser<T> {
#[inline]
pub fn parse(&self, lexer: &mut StLexer) -> Result<Declaration, ParseError> {
pub fn name(&self) -> String {
self.inner.name()
}

#[inline]
pub fn parse_pou(&self, lexer: &mut StLexer) -> Result<(Declaration, Statement), ParseError> {
self.inner.parse_pou(lexer)
}

#[inline]
pub fn parse_decl(&self, lexer: &mut StLexer) -> Result<Declaration, ParseError> {
self.inner.parse_decl(lexer)
}

Expand Down Expand Up @@ -118,6 +128,8 @@ impl ParserBuilder {
}

pub trait ParserTrait {
fn name(&self) -> String;
fn parse_pou(&self, lexer: &mut StLexer) -> Result<(Declaration, Statement), ParseError>;
fn parse_decl(&self, lexer: &mut StLexer) -> Result<Declaration, ParseError>;
fn parse_stmt(&self, lexer: &mut StLexer) -> Result<Statement, ParseError>;
fn parse_literal(&self, lexer: &mut StLexer) -> Result<LiteralExpression, ParseError>;
Expand Down
5 changes: 4 additions & 1 deletion lib/src/serde/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ impl From<Project> for ModuleContext {

for pou in proj.pou_list.pou {
let mut lexer = StLexerBuilder::new().build_str(&pou.interface.content);
let decl = ParserBuilder::default().build().parse(&mut lexer).unwrap();
let decl = ParserBuilder::default()
.build()
.parse_decl(&mut lexer)
.unwrap();
let func = ctx_write.add_declaration(
decl,
pou.uuid_text
Expand Down
9 changes: 6 additions & 3 deletions lib/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn test_decl_parse() {
let code = fs::read_to_string(&f).unwrap();

let mut lexer = StLexerBuilder::new().build_str(&code);
match parser.parse(&mut lexer) {
match parser.parse_decl(&mut lexer) {
Ok(_) => assert!(lexer.eof(), "{}", f.display()),
Err(e) => panic!("{}: {:?}", f.display(), e),
}
Expand Down Expand Up @@ -60,14 +60,17 @@ fn test_scope_lookup() {
let app_ctx = mgr.write().get_context(app_id).unwrap();
let mut global =
StLexerBuilder::new().build_str("VAR_GLOBAL END_VAR VAR_GLOBAL g1: REAL; END_VAR");
let global = ParserBuilder::default().build().parse(&mut global).unwrap();
let global = ParserBuilder::default()
.build()
.parse_decl(&mut global)
.unwrap();
let _global_id = app_ctx.write().add_declaration(global, Uuid::nil());

let mut test_func =
StLexerBuilder::new().build_str("program prg: int VAR g1: BYTE; END_VAR end_program");
let test_fun_decl = ParserBuilder::default()
.build()
.parse(&mut test_func)
.parse_decl(&mut test_func)
.unwrap();
let test_fun_decl_id = app_ctx.write().add_declaration(test_fun_decl, Uuid::nil());

Expand Down
2 changes: 1 addition & 1 deletion lib/src/test/test_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() {
let mut lexer = StLexerBuilder::new().build_file(f).unwrap();

let parser = ParserBuilder::default().build();
match parser.parse(&mut lexer) {
match parser.parse_decl(&mut lexer) {
Ok(r) => {
println!("{:?}", r);
}
Expand Down
11 changes: 10 additions & 1 deletion lsp/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::lsp_types::{TokenModifiers, TokenTypes};
use dashmap::DashMap;
use ropey::Rope;
use serde_json::Value;
use stc::parser::{StLexerBuilder, TokenKind};
use stc::parser::{ParserBuilder, StLexerBuilder, TokenKind};
use stc::prelude::UnitsManager;
use strum::IntoEnumIterator;
use tower_lsp::jsonrpc::Result;
Expand Down Expand Up @@ -50,6 +50,15 @@ impl StcLsp {
pub fn on_file_change(&self, url: &Url, text: String) {
let rope = text.into();
self.src_mgr.insert(url.clone(), rope);

let rope_data = self.src_mgr.get(url).unwrap();
let code = rope_data.value();

let mut lexer = StLexerBuilder::default().build_iter(code.chars());
let parser = ParserBuilder::default().build();

let (_decl, _body) = parser.parse_pou(&mut lexer).unwrap();
// TODO: save parse results
}
}

Expand Down

0 comments on commit c1cb1a8

Please sign in to comment.