Skip to content

Commit df4692e

Browse files
committed
Add ProgramBuilder::add_pcode(path) and --pcode CLI
1 parent 044fe78 commit df4692e

116 files changed

Lines changed: 193 additions & 14 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎crates/cli/src/main.rs‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,23 @@ struct Opt {
1111
/// A loose script (not class file) to add to the compiled program.
1212
///
1313
/// Multiple scripts may be added to one program.
14-
/// Scripts will be executed in order that they are added, before any classes are loaded.
14+
/// Scripts will be executed in order that they are added, before any classes are loaded and after any raw pcode.
1515
///
1616
/// It is valid to not specify any scripts - if so, the entry point of the program will be the first
1717
/// class (see `--class`) that contains a `static function main()` method.
1818
#[arg(name = "SCRIPT", help_heading = "Inputs")]
1919
script: Vec<PathBuf>,
2020

21+
/// Path to some raw pcode to add to the compiled program.
22+
///
23+
/// Multiple pcodes may be added to one program.
24+
/// Pcodes will be executed in order that they are added, before any scripts are executed.
25+
///
26+
/// It is valid to not specify any scripts - if so, the entry point of the program will be the first
27+
/// class (see `--class`) that contains a `static function main()` method.
28+
#[arg(long, help_heading = "Inputs")]
29+
pcode: Vec<PathBuf>,
30+
2131
/// Output file path. This will be overwritten if it already exists.
2232
///
2333
/// If not specified:
@@ -71,8 +81,11 @@ fn main() -> Result<()> {
7181
let provider = FileSystemSourceProvider::with_roots(opt.classpath);
7282
let mut builder = ProgramBuilder::new(provider);
7383

84+
for src in &opt.pcode {
85+
builder.add_pcode(&src.to_string_lossy());
86+
}
7487
for src in &opt.script {
75-
builder.add_script(&src.file_name().unwrap().to_string_lossy());
88+
builder.add_script(&src.to_string_lossy());
7689
}
7790
for class in &opt.class {
7891
builder.add_class(class);

‎crates/rascal/src/internal/as2/error.rs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,16 @@ impl From<ParseError<Tokens<'_>, ContextError>> for ParsingError {
3131
}
3232
}
3333
}
34+
35+
impl From<ParseError<crate::internal::as2_pcode::Tokens<'_>, ContextError>> for ParsingError {
36+
fn from(error: ParseError<crate::internal::as2_pcode::Tokens, ContextError>) -> Self {
37+
Self {
38+
error: error.inner().to_string(),
39+
span: error
40+
.input()
41+
.get(error.offset())
42+
.map(|t| t.span)
43+
.unwrap_or_default(),
44+
}
45+
}
46+
}

‎crates/rascal/src/internal/as2_pcode.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use crate::internal::as2_pcode::lexer::Lexer;
1+
pub use crate::internal::as2_pcode::lexer::Lexer;
22
use crate::internal::as2_pcode::lexer::tokens::Token;
33
use crate::internal::as2_pcode::parser::PCodeError;
4+
pub use crate::internal::as2_pcode::parser::Tokens;
5+
pub use crate::internal::as2_pcode::parser::parse_actions;
46
pub use crate::internal::as2_pcode::pcode::Action;
57
pub use crate::internal::as2_pcode::pcode::Actions;
68
pub use crate::internal::as2_pcode::pcode::CatchTarget;

‎crates/rascal/src/internal/as2_pcode/parser.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ mod error;
88
#[cfg(test)]
99
mod tests;
1010

11-
pub(crate) type Tokens<'i> = TokenSlice<'i, Token<'i>>;
11+
pub type Tokens<'i> = TokenSlice<'i, Token<'i>>;
1212

1313
pub use crate::internal::as2_pcode::parser::error::PCodeError;
14-
use crate::internal::as2_pcode::pcode::Actions;
14+
pub use crate::internal::as2_pcode::pcode::Actions;
1515

1616
pub fn parse_actions<'a>(
1717
source: &'a [Token],

‎crates/rascal/src/program.rs‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct Program {
1414
pub(crate) initial_script: Vec<hir::StatementKind>,
1515
pub(crate) interfaces: Vec<hir::Interface>,
1616
pub(crate) classes: Vec<hir::Class>,
17+
pub(crate) custom_pcodes: Vec<Actions>,
1718
}
1819

1920
impl Program {
@@ -36,6 +37,7 @@ impl Program {
3637
initializer,
3738
extra_modules,
3839
swf_version,
40+
custom_pcodes: self.custom_pcodes.clone(),
3941
}
4042
}
4143
}
@@ -45,6 +47,7 @@ pub struct CompiledProgram {
4547
pub(crate) initializer: Option<Actions>,
4648
pub(crate) extra_modules: Vec<(String, Actions)>,
4749
pub(crate) swf_version: u8,
50+
pub(crate) custom_pcodes: Vec<Actions>,
4851
}
4952

5053
impl CompiledProgram {
@@ -56,6 +59,7 @@ impl CompiledProgram {
5659
pub struct ProgramBuilder<P> {
5760
provider: P,
5861
scripts: Vec<String>,
62+
pcodes: Vec<String>,
5963
classes: Vec<String>,
6064
}
6165

@@ -64,6 +68,10 @@ impl<P> ProgramBuilder<P> {
6468
self.scripts.push(path.to_owned());
6569
}
6670

71+
pub fn add_pcode(&mut self, path: &str) {
72+
self.pcodes.push(path.to_owned());
73+
}
74+
6775
pub fn add_class(&mut self, path: &str) {
6876
self.classes.push(path.to_owned());
6977
}
@@ -74,6 +82,7 @@ impl<P: SourceProvider> ProgramBuilder<P> {
7482
Self {
7583
provider,
7684
scripts: vec![],
85+
pcodes: vec![],
7786
classes: vec![],
7887
}
7988
}
@@ -85,8 +94,9 @@ impl<P: SourceProvider> ProgramBuilder<P> {
8594
let mut errors = ErrorSet::new();
8695
let mut loaded_classes = IndexSet::new();
8796
let mut pending_classes = self.classes;
97+
let mut custom_pcodes = vec![];
8898

89-
fn load_file<P: SourceProvider>(
99+
fn load_actionscript<P: SourceProvider>(
90100
provider: &P,
91101
errors: &mut ErrorSet,
92102
loaded_classes: &mut IndexSet<String>,
@@ -126,8 +136,32 @@ impl<P: SourceProvider> ProgramBuilder<P> {
126136
Some(hir)
127137
}
128138

139+
fn load_pcode<P: SourceProvider>(
140+
provider: &P,
141+
errors: &mut ErrorSet,
142+
path: &str,
143+
custom_pcodes: &mut Vec<Actions>,
144+
) {
145+
let source = match provider.load(path) {
146+
Ok(source) => source,
147+
Err(e) => {
148+
errors.add_io_error(path, e);
149+
return;
150+
}
151+
};
152+
let tokens = crate::internal::as2_pcode::Lexer::new(&source).into_vec();
153+
match crate::internal::as2_pcode::parse_actions(&tokens) {
154+
Ok(actions) => custom_pcodes.push(actions),
155+
Err(e) => errors.add_parsing_error(path, &source, e.into()),
156+
}
157+
}
158+
159+
for path in self.pcodes {
160+
load_pcode(&self.provider, &mut errors, &path, &mut custom_pcodes);
161+
}
162+
129163
for path in self.scripts {
130-
if let Some(document) = load_file(
164+
if let Some(document) = load_actionscript(
131165
&self.provider,
132166
&mut errors,
133167
&mut loaded_classes,
@@ -145,7 +179,7 @@ impl<P: SourceProvider> ProgramBuilder<P> {
145179

146180
while let Some(type_name) = pending_classes.pop() {
147181
let filename = type_path_to_file_path(&type_name);
148-
if let Some(document) = load_file(
182+
if let Some(document) = load_actionscript(
149183
&self.provider,
150184
&mut errors,
151185
&mut loaded_classes,
@@ -186,6 +220,7 @@ impl<P: SourceProvider> ProgramBuilder<P> {
186220
initial_script,
187221
interfaces,
188222
classes,
223+
custom_pcodes,
189224
})
190225
}
191226
}

‎crates/rascal/src/snapshots/rascal__program__tests__all_samples@AnEmptyInterface.as.snap‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ Ok:
1010
extends: ~
1111
functions: {}
1212
classes: []
13+
custom_pcodes: []

‎crates/rascal/src/snapshots/rascal__program__tests__all_samples@AnExtendingInterface.as.snap‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ Ok:
2121
extends: ~
2222
functions: {}
2323
classes: []
24+
custom_pcodes: []

‎crates/rascal/src/snapshots/rascal__program__tests__all_samples@ClassMembersTest.as.snap‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,3 +979,4 @@ Ok:
979979
args: []
980980
return_type: ~
981981
body: []
982+
custom_pcodes: []

‎crates/rascal/src/snapshots/rascal__program__tests__all_samples@DynamicClass.as.snap‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ Ok:
2323
args: []
2424
return_type: ~
2525
body: []
26+
custom_pcodes: []

‎crates/rascal/src/snapshots/rascal__program__tests__all_samples@EmptyClass.as.snap‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ Ok:
2323
args: []
2424
return_type: ~
2525
body: []
26+
custom_pcodes: []

0 commit comments

Comments
 (0)