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

Add language support for RP2040 PIO ASM #1056

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ Perl
Perl6
Pest
Php
PioAsm
Poke
Polly
Pony
Expand Down
7 changes: 7 additions & 0 deletions languages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,13 @@
"quotes": [["\\\"", "\\\""], ["'", "'"]],
"extensions": ["php"]
},
"PioAsm": {
"name": "RP2040 PIO ASM",
"line_comment": [";", "//"],
"multi_line_comments": [["/*", "*/"]],
"important_syntax": ["% c-sdk {"],
"extensions": ["pio"]
},
"Poke": {
"multi_line_comments": [["/*", "*/"]],
"extensions": ["pk"]
Expand Down
25 changes: 25 additions & 0 deletions src/language/embedding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub static END_TEMPLATE: Lazy<Regex> = Lazy::new(|| Regex::new(r#"</template>"#)
pub static STARTING_MARKDOWN_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r#"```\S+\s"#).unwrap());
pub static ENDING_MARKDOWN_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r#"```\s?"#).unwrap());

pub static START_PIO_CSDK: Lazy<Regex> = Lazy::new(|| Regex::new(r#"% c-sdk \{"#).unwrap());
pub static END_PIO_CSDK: Lazy<Regex> = Lazy::new(|| Regex::new(r#"%}"#).unwrap());

/// A memory of a regex matched.
/// The values provided by `Self::start` and `Self::end` are in the same space as the
/// start value supplied to `RegexCache::build`
Expand Down Expand Up @@ -62,6 +65,7 @@ pub(crate) struct RegexCache<'a> {
pub(crate) enum RegexFamily<'a> {
HtmlLike(HtmlLike<'a>),
Markdown(Markdown<'a>),
PioAsm(PioAsm<'a>),
Rust,
}

Expand All @@ -75,6 +79,10 @@ pub(crate) struct Markdown<'a> {
starts: Option<Box<[Capture<'a>]>>,
}

pub(crate) struct PioAsm<'a> {
starts: Option<Box<[Capture<'a>]>>,
}

impl<'a> HtmlLike<'a> {
pub fn start_script_in_range(
&'a self,
Expand Down Expand Up @@ -107,6 +115,12 @@ impl<'a> Markdown<'a> {
}
}

impl<'a> PioAsm<'a> {
pub fn starts_in_range(&'a self, start: usize, end: usize) -> Option<&Capture<'a>> {
filter_range(self.starts.as_ref()?, start, end).and_then(|mut it| it.next())
}
}

fn filter_range<'a>(
dataset: &'a [Capture<'a>],
start: usize,
Expand Down Expand Up @@ -169,6 +183,17 @@ impl<'a> RegexCache<'a> {
None
}
}
LanguageType::PioAsm => {
let pioasm = PioAsm {
starts: save_captures(&START_PIO_CSDK, lines, start, end),
};

if pioasm.starts.is_some() {
Some(RegexFamily::PioAsm(pioasm))
} else {
None
}
}
_ => None,
};
Self { inner }
Expand Down
6 changes: 6 additions & 0 deletions src/language/language_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ impl LanguageType {
// Add all the markdown blobs.
*stats.blobs.entry(language).or_default() += blob;
}
LanguageContext::PioAsm { balanced, language } => {
// Add the lines for the code fences.
stats.comments += if balanced { 2 } else { 1 };
// Add the code inside the fence to the stats.
*stats.blobs.entry(language).or_default() += blob;
}
}

// Advance to after the language code and the delimiter..
Expand Down
40 changes: 40 additions & 0 deletions src/language/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use once_cell::sync::Lazy;

use super::embedding::{
RegexCache, RegexFamily, ENDING_MARKDOWN_REGEX, END_SCRIPT, END_STYLE, END_TEMPLATE,
END_PIO_CSDK,
};
use crate::{stats::CodeStats, utils::ext::SliceExt, Config, LanguageType};

Expand Down Expand Up @@ -57,6 +58,10 @@ pub(crate) enum LanguageContext {
balanced: bool,
language: LanguageType,
},
PioAsm {
balanced: bool,
language: LanguageType,
},
Rust,
}

Expand Down Expand Up @@ -517,6 +522,41 @@ impl SyntaxCounter {
None
}
}
RegexFamily::PioAsm(pioasm) => {
// TODO: This logic is copied from Markdown. That is, it allows for unbalanced code
// blocks, which I don't think is valid. Not sure what the proper way to handle
// this is, though.
if !lines[start..end].contains_slice(b"% c-sdk {") {
return None;
}

let opening_fence = pioasm.starts_in_range(start, end)?;
let start_of_code = opening_fence.end();
let closing_fence = END_PIO_CSDK.find(&lines[start_of_code..]);
if let Some(m) = &closing_fence {
trace!("{:?}", String::from_utf8_lossy(m.as_bytes()));
}
let end_of_code = closing_fence
.map_or_else(|| lines.len(), |fence| start_of_code + fence.start());
let end_of_code_block =
closing_fence.map_or_else(|| lines.len(), |fence| start_of_code + fence.end());
let balanced = closing_fence.is_some();

let language = LanguageType::C;
trace!(
"{} BLOCK: {:?}",
language,
String::from_utf8_lossy(&lines[start_of_code..end_of_code])
);
let stats =
language.parse_from_slice(&lines[start_of_code..end_of_code].trim(), config);

Some(FileContext::new(
LanguageContext::PioAsm { balanced, language },
end_of_code_block,
stats,
))
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions tests/data/pioasm.pio
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
; 30 lines 9 code 14 comments 7 blanks
; test program
.program hello

; A comment.
// A C++-style comment
/* A C-style comment */

/*
*
* a multiline comment
*/

loop:
pull ; pull word from TX FIFO
out pins, 1 /* set pins */
jmp loop // jump

% c-sdk {
/* A dummy function to test pioasm parsing. */
static inline uint my_cool_function(uint a) {
uint ret = a >> 1;

// a comment in C
return ret;
}
%}

; a comment after the C code