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

Commit 741fed6

Browse files
committed
Merge branch 'CurrySoftware/master'.
This merge commit patches the feature branch to support the recently added visitor feature from master.
2 parents a9136a8 + 5dfb9c4 commit 741fed6

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

src/parsers.rs

+43-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,31 @@ 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".into(),
3221+
value: syntax::Expr::DoubleConst(1.0)
3222+
})));
3223+
3224+
assert_eq!(preprocessor(&b"#define test123 .0f\n"[..]),
3225+
IResult::Done(&b""[..],
3226+
syntax::Preprocessor::Define(syntax::PreprocessorDefine {
3227+
name: "test123".into(),
3228+
value: syntax::Expr::FloatConst(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".into(),
3235+
value: syntax::Expr::IntConst(1)
3236+
})));
3237+
}
3238+
31973239
#[test]
31983240
fn parse_pp_extension_name() {
31993241
assert_eq!(pp_extension_name(&b"all"[..]), IResult::Done(&b""[..], syntax::PreprocessorExtensionName::All));

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 {

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

src/visitor.rs

+16
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ pub trait Visitor {
8484
Visit::Children
8585
}
8686

87+
fn visit_preprocessor_define(&mut self, _: &mut syntax::PreprocessorDefine) -> Visit {
88+
Visit::Children
89+
}
90+
8791
fn visit_preprocessor_extension(&mut self, _: &mut syntax::PreprocessorExtension) -> Visit {
8892
Visit::Children
8993
}
@@ -306,13 +310,25 @@ impl Host for syntax::Preprocessor {
306310

307311
if visit == Visit::Children {
308312
match *self {
313+
syntax::Preprocessor::Define(ref mut pd) => pd.visit(visitor),
309314
syntax::Preprocessor::Version(ref mut pv) => pv.visit(visitor),
310315
syntax::Preprocessor::Extension(ref mut ext) => ext.visit(visitor)
311316
}
312317
}
313318
}
314319
}
315320

321+
impl Host for syntax::PreprocessorDefine {
322+
fn visit<V>(&mut self, visitor: &mut V) where V: Visitor {
323+
let visit = visitor.visit_preprocessor_define(self);
324+
325+
if visit == Visit::Children {
326+
self.name.visit(visitor);
327+
self.value.visit(visitor);
328+
}
329+
}
330+
}
331+
316332
impl Host for syntax::PreprocessorVersion {
317333
fn visit<V>(&mut self, visitor: &mut V) where V: Visitor {
318334
let visit = visitor.visit_preprocessor_version(self);

0 commit comments

Comments
 (0)