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

Parser recovery #187

Merged
merged 1 commit into from
Jul 17, 2021
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
6 changes: 1 addition & 5 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
image:
file: .devcontainer/Dockerfile
vscode:
extensions:
- vadimcn.vscode-lldb@1.6.0:tbQLAVn9tjDxr5QEpv1W+A==
- tamasfe.even-better-toml@0.9.1:0QCvay8L3oAOZQY/5DkpSg==
file: .devcontainer/Dockerfile
31 changes: 31 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ structopt = "0.3"
indexmap = "1.6"
chrono = "0.4"
glob = "0.3.0"
codespan-reporting = "0.11.1"


[lib]
name = "rusty"
Expand Down
9 changes: 4 additions & 5 deletions examples/simple_program.st
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
PROGRAM hello
VAR

END_VAR
END_PROGRAM
TYPE MyType:
PROGRAM
END_PROGRAM
END_TYPE
122 changes: 28 additions & 94 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,27 +159,16 @@ impl Variable {

#[derive(Clone, Debug, PartialEq)]
pub struct SourceRange {
file_path: String,
range: core::ops::Range<usize>,
}

impl SourceRange {
pub fn new(file_path: &str, range: core::ops::Range<usize>) -> SourceRange {
SourceRange {
file_path: file_path.into(),
range,
}
pub fn new(range: core::ops::Range<usize>) -> SourceRange {
SourceRange { range }
}

pub fn undefined() -> SourceRange {
SourceRange {
file_path: "".into(),
range: 0..0,
}
}

pub fn get_file_path(&self) -> &str {
&self.file_path
SourceRange { range: 0..0 }
}

pub fn get_start(&self) -> usize {
Expand All @@ -189,68 +178,15 @@ impl SourceRange {
pub fn get_end(&self) -> usize {
self.range.end
}
}

impl From<std::ops::Range<usize>> for SourceRange {
fn from(range: std::ops::Range<usize>) -> SourceRange {
SourceRange::new("", range)
pub fn sub_range(&self, start: usize, len: usize) -> SourceRange {
SourceRange::new((self.get_start() + start)..(self.get_start() + len))
}
}

#[derive(Clone, Debug, PartialEq)]
pub struct NewLines {
new_lines: Vec<usize>,
}

impl NewLines {
pub fn new(source: &str) -> NewLines {
let mut new_lines = vec![0];
for (offset, c) in source.char_indices() {
if c == '\n' {
new_lines.push(offset);
}
}
NewLines { new_lines }
}

/// binary search the first element which is bigger than the given index
/// starting with line 1
pub fn get_line_of(&self, offset: usize) -> Option<usize> {
if offset == 0 {
return Some(1);
}

let mut start = 0;
let mut end = self.new_lines.len() - 1;
let mut result: usize = 0;
while start <= end {
let mid = (start + end) / 2;

if self.new_lines[mid] <= offset {
start = mid + 1; //move to the right
} else {
result = mid;
end = mid - 1;
}
}

if self.new_lines[result] > offset {
Some(result)
} else {
None
}
}

/// get the offset of the new_line that starts line l (starting with line 1)
pub fn get_offest_of_line(&self, l: usize) -> usize {
self.new_lines[l - 1]
}

pub fn _get_location_information(&self, offset: &core::ops::Range<usize>) -> String {
let line = self.get_line_of(offset.start).unwrap_or(1);
let line_offset = self.get_offest_of_line(line);
let offset = offset.start - line_offset..offset.end - line_offset;
format!("line: {:}, offset: {:?}", line, offset)
impl From<std::ops::Range<usize>> for SourceRange {
fn from(range: std::ops::Range<usize>) -> SourceRange {
SourceRange::new(range)
}
}

Expand Down Expand Up @@ -421,6 +357,9 @@ impl Debug for ConditionalBlock {

#[derive(Clone, PartialEq)]
pub enum Statement {
EmptyStatement {
location: SourceRange,
},
// Literals
LiteralInteger {
value: String,
Expand Down Expand Up @@ -557,11 +496,15 @@ pub enum Statement {
else_block: Vec<Statement>,
location: SourceRange,
},
CaseCondition {
condition: Box<Statement>,
},
}

impl Debug for Statement {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self {
Statement::EmptyStatement { .. } => f.debug_struct("EmptyStatement").finish(),
Statement::LiteralInteger { value, .. } => f
.debug_struct("LiteralInteger")
.field("value", value)
Expand Down Expand Up @@ -758,6 +701,10 @@ impl Debug for Statement {
.field("multiplier", multiplier)
.field("element", element)
.finish(),
Statement::CaseCondition { condition } => f
.debug_struct("CaseCondition")
.field("condition", condition)
.finish(),
}
}
}
Expand All @@ -773,6 +720,7 @@ impl Statement {
}
pub fn get_location(&self) -> SourceRange {
match self {
Statement::EmptyStatement { location, .. } => location.clone(),
Statement::LiteralInteger { location, .. } => location.clone(),
Statement::LiteralDate { location, .. } => location.clone(),
Statement::LiteralDateAndTime { location, .. } => location.clone(),
Expand All @@ -790,15 +738,12 @@ impl Statement {
let last = elements
.last()
.map_or_else(SourceRange::undefined, |it| it.get_location());
SourceRange::new(first.get_file_path(), first.get_start()..last.get_end())
SourceRange::new(first.get_start()..last.get_end())
}
Statement::BinaryExpression { left, right, .. } => {
let left_loc = left.get_location();
let right_loc = right.get_location();
SourceRange::new(
&left_loc.file_path,
left_loc.range.start..right_loc.range.end,
)
SourceRange::new(left_loc.range.start..right_loc.range.end)
}
Statement::UnaryExpression { location, .. } => location.clone(),
Statement::ExpressionList { expressions } => {
Expand All @@ -808,31 +753,22 @@ impl Statement {
let last = expressions
.last()
.map_or_else(SourceRange::undefined, |it| it.get_location());
SourceRange::new(first.get_file_path(), first.get_start()..last.get_end())
SourceRange::new(first.get_start()..last.get_end())
}
Statement::RangeStatement { start, end } => {
let start_loc = start.get_location();
let end_loc = end.get_location();
SourceRange::new(
&start_loc.file_path,
start_loc.range.start..end_loc.range.end,
)
SourceRange::new(start_loc.range.start..end_loc.range.end)
}
Statement::Assignment { left, right } => {
let left_loc = left.get_location();
let right_loc = right.get_location();
SourceRange::new(
&left_loc.file_path,
left_loc.range.start..right_loc.range.end,
)
SourceRange::new(left_loc.range.start..right_loc.range.end)
}
Statement::OutputAssignment { left, right } => {
let left_loc = left.get_location();
let right_loc = right.get_location();
SourceRange::new(
&left_loc.file_path,
left_loc.range.start..right_loc.range.end,
)
SourceRange::new(left_loc.range.start..right_loc.range.end)
}
Statement::CallStatement { location, .. } => location.clone(),
Statement::IfStatement { location, .. } => location.clone(),
Expand All @@ -843,12 +779,10 @@ impl Statement {
Statement::ArrayAccess { reference, access } => {
let reference_loc = reference.get_location();
let access_loc = access.get_location();
SourceRange::new(
&reference_loc.file_path,
reference_loc.range.start..access_loc.range.end,
)
SourceRange::new(reference_loc.range.start..access_loc.range.end)
}
Statement::MultipliedStatement { location, .. } => location.clone(),
Statement::CaseCondition { condition } => condition.get_location(),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/codegen/generators/statement_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ impl<'a, 'b> StatementCodeGenerator<'a, 'b> {
/// - `statement` the statement to be generated
pub fn generate_statement(&self, statement: &Statement) -> Result<(), CompileError> {
match statement {
Statement::EmptyStatement { .. } => {
//nothing to generate
}
Statement::Assignment { left, right } => {
self.generate_assignment_statement(left, right)?;
}
Expand Down
8 changes: 4 additions & 4 deletions src/codegen/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ mod typesystem_test;
#[macro_export]
macro_rules! codegen_wihout_unwrap {
($code:tt) => {{
let lexer = crate::lexer::lex("", $code);
let (mut ast, _) = crate::parser::parse(lexer).unwrap();
let lexer = crate::lexer::lex($code);
let (mut ast, ..) = crate::parser::parse(lexer).unwrap();

let context = inkwell::context::Context::create();
crate::ast::pre_process(&mut ast);
Expand All @@ -20,8 +20,8 @@ macro_rules! codegen_wihout_unwrap {
#[macro_export]
macro_rules! codegen {
($code:tt) => {{
let lexer = crate::lexer::lex("", $code);
let (mut ast, _) = crate::parser::parse(lexer).unwrap();
let lexer = crate::lexer::lex($code);
let (mut ast, ..) = crate::parser::parse(lexer).unwrap();

let context = inkwell::context::Context::create();
crate::ast::pre_process(&mut ast);
Expand Down
26 changes: 26 additions & 0 deletions src/codegen/tests/code_gen_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@ END_PROGRAM
assert_eq!(result, expected);
}

#[test]
fn empty_statements_dont_generate_anything() {
let result = codegen!(
r#"PROGRAM prg
VAR x : DINT; y : DINT; END_VAR
x;
;;;;
y;
END_PROGRAM
"#
);
let expected = generate_program_boiler_plate(
"prg",
&[("i32", "x"), ("i32", "y")],
"void",
"",
"",
r#"%load_x = load i32, i32* %x, align 4
%load_y = load i32, i32* %y, align 4
ret void
"#,
);

assert_eq!(result, expected);
}

#[test]
fn empty_global_variable_list_generates_nothing() {
let result = generate_with_empty_program!("VAR_GLOBAL END_VAR");
Expand Down
7 changes: 7 additions & 0 deletions src/compile_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use thiserror::Error;

use crate::ast::SourceRange;
use crate::Diagnostic;

#[derive(Error, Debug, PartialEq)]
pub enum CompileError {
Expand Down Expand Up @@ -37,6 +38,12 @@ pub enum CompileError {
IoError { path: String, reason: String },
}

impl From<Diagnostic> for CompileError {
fn from(diag: Diagnostic) -> Self {
CompileError::codegen_error(diag.get_message().into(), diag.get_location())
}
}

impl CompileError {
pub fn missing_function(location: SourceRange) -> CompileError {
CompileError::MissingFunctionError { location }
Expand Down
Loading