Skip to content
This repository was archived by the owner on Feb 6, 2025. It is now read-only.

Commit 5dfb9c4

Browse files
committed
implement #define parsing
1 parent 2fcce27 commit 5dfb9c4

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

Diff for: src/parsers.rs

+44-1
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,7 @@ named!(pub translation_unit<&[u8], syntax::TranslationUnit>,
14861486
/// Parse a preprocessor command.
14871487
named!(pub preprocessor<&[u8], syntax::Preprocessor>,
14881488
bl!(alt!(
1489+
map!(pp_define, syntax::Preprocessor::Define) |
14891490
map!(pp_version, syntax::Preprocessor::Version) |
14901491
map!(pp_extension, syntax::Preprocessor::Extension)
14911492
))
@@ -1514,6 +1515,23 @@ macro_rules! ppws {
15141515
}}
15151516
}
15161517

1518+
/// Parse a #define
1519+
named!(pub pp_define<&[u8], syntax::PreprocessorDefine>,
1520+
ppws!(do_parse!(
1521+
char!('#') >>
1522+
tag!("define") >>
1523+
name: identifier >>
1524+
value: primary_expr>>
1525+
char!('\n') >>
1526+
1527+
(syntax::PreprocessorDefine {
1528+
name: name,
1529+
value: value
1530+
})
1531+
))
1532+
);
1533+
1534+
15171535
/// Parse a #version.
15181536
named!(pub pp_version<&[u8], syntax::PreprocessorVersion>,
15191537
ppws!(do_parse!(
@@ -3193,7 +3211,32 @@ mod tests {
31933211
profile: Some(syntax::PreprocessorVersionProfile::Core)
31943212
})));
31953213
}
3196-
3214+
3215+
#[test]
3216+
fn parse_define() {
3217+
assert_eq!(preprocessor(&b"#define test 1.0\n"[..]),
3218+
IResult::Done(&b""[..],
3219+
syntax::Preprocessor::Define(syntax::PreprocessorDefine {
3220+
name: "test".to_string(),
3221+
value: syntax::Expr::DoubleConst(1.0)
3222+
})));
3223+
3224+
assert_eq!(preprocessor(&b"#define test123 .0\n"[..]),
3225+
IResult::Done(&b""[..],
3226+
syntax::Preprocessor::Define(syntax::PreprocessorDefine {
3227+
name: "test123".to_string(),
3228+
value: syntax::Expr::DoubleConst(0.0)
3229+
})));
3230+
3231+
assert_eq!(preprocessor(&b"#define test 1\n"[..]),
3232+
IResult::Done(&b""[..],
3233+
syntax::Preprocessor::Define(syntax::PreprocessorDefine {
3234+
name: "test".to_string(),
3235+
value: syntax::Expr::IntConst(1)
3236+
})));
3237+
}
3238+
3239+
31973240
#[test]
31983241
fn parse_pp_extension_name() {
31993242
assert_eq!(pp_extension_name(&b"all"[..]), IResult::Done(&b""[..], syntax::PreprocessorExtensionName::All));

Diff for: src/syntax.rs

+9
Original file line numberDiff line numberDiff line change
@@ -863,10 +863,19 @@ pub enum JumpStatement {
863863
/// > added in the future. Stay tuned.
864864
#[derive(Clone, Debug, PartialEq)]
865865
pub enum Preprocessor {
866+
Define(PreprocessorDefine),
866867
Version(PreprocessorVersion),
867868
Extension(PreprocessorExtension)
868869
}
869870

871+
/// A #define preprocessor command.
872+
/// Allows any expression but only Integer and Float literals make sense
873+
#[derive(Clone, Debug, PartialEq)]
874+
pub struct PreprocessorDefine {
875+
pub name: Identifier,
876+
pub value: Expr,
877+
}
878+
870879
/// A #version preprocessor command.
871880
#[derive(Clone, Debug, PartialEq)]
872881
pub struct PreprocessorVersion {

Diff for: src/transpiler/glsl.rs

+7
Original file line numberDiff line numberDiff line change
@@ -817,11 +817,18 @@ pub fn show_jump_statement<F>(f: &mut F, j: &syntax::JumpStatement) where F: Wri
817817

818818
pub fn show_preprocessor<F>(f: &mut F, pp: &syntax::Preprocessor) where F: Write {
819819
match *pp {
820+
syntax::Preprocessor::Define(ref pd) => show_preprocessor_define(f, pd),
820821
syntax::Preprocessor::Version(ref pv) => show_preprocessor_version(f, pv),
821822
syntax::Preprocessor::Extension(ref pe) => show_preprocessor_extension(f, pe)
822823
}
823824
}
824825

826+
pub fn show_preprocessor_define<F>(f: &mut F, pd: &syntax::PreprocessorDefine) where F: Write {
827+
let _ = write!(f, "#define {} ", pd.name);
828+
show_expr(f, &pd.value);
829+
let _ = f.write_str("\n");
830+
}
831+
825832
pub fn show_preprocessor_version<F>(f: &mut F, pv: &syntax::PreprocessorVersion) where F: Write {
826833
let _ = write!(f, "#version {}", pv.version);
827834

0 commit comments

Comments
 (0)