Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cc5ed95

Browse files
committed
Fix double right brack and dumps in parser
1 parent 3d516ae commit cc5ed95

File tree

15 files changed

+306
-30
lines changed

15 files changed

+306
-30
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ name = "cpp_parser"
2424
path = "src/lib.rs"
2525

2626
[workspace]
27-
members = ["cli_macro_stats"]
27+
members = ["cli_macro_stats", "cli_ast"]
2828

2929
[profile.release]
3030
lto = true

cli_ast/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "ast"
3+
version = "0.0.1"
4+
authors = ["calixteman <[email protected]>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
clap = "2.33"
9+
cpp-parser = { path = ".." }

cli_ast/src/main.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
2+
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
3+
// http://opensource.org/licenses/MIT>, at your option. This file may not be
4+
// copied, modified, or distributed except according to those terms.
5+
6+
#[macro_use]
7+
extern crate clap;
8+
9+
use clap::{App, Arg};
10+
use cpp_parser::args::{Language, PreprocOptions};
11+
use cpp_parser::defaults;
12+
use cpp_parser::lexer::preprocessor::cache::IfCache;
13+
use cpp_parser::lexer::preprocessor::context::{DefaultContext, PreprocContext};
14+
use cpp_parser::lexer::source::{self, FileId, SourceMutex};
15+
use cpp_parser::lexer::{Lexer, TLexer};
16+
use cpp_parser::parser::{Context, Dump, Unit, UnitParser};
17+
use std::path::PathBuf;
18+
use std::sync::{Arc, Mutex};
19+
20+
fn main() {
21+
let matches = App::new("AST dump")
22+
.version(crate_version!())
23+
.author(&*env!("CARGO_PKG_AUTHORS").replace(':', "\n"))
24+
.about("Report macro use")
25+
.arg(
26+
Arg::with_name("file")
27+
.help("File to dump")
28+
.takes_value(true),
29+
)
30+
.get_matches();
31+
32+
let file = matches.value_of("file").unwrap().to_string();
33+
34+
let source = source::get_source_mutex();
35+
let if_cache = Arc::new(IfCache::default());
36+
let opt = PreprocOptions {
37+
def: defaults::get_defined(),
38+
sys_paths: defaults::get_sys_paths(),
39+
includes: vec![],
40+
current_dir: PathBuf::from("."),
41+
file: PathBuf::from(""),
42+
lang: Language::CPP,
43+
};
44+
45+
let lexer = Lexer::<DefaultContext>::new_from_file(&file, source, if_cache, opt);
46+
47+
let context = Context::default();
48+
let mut parser = UnitParser { lexer, context };
49+
50+
match parser.parse() {
51+
Ok(unit) => {
52+
unit.dump_me();
53+
}
54+
Err(e) => {
55+
eprintln!("{:?}", e);
56+
}
57+
}
58+
}

cli_macro_stats/src/main.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,6 @@ impl PreprocContext for StatsContext {
168168
stats: HashMap::default(),
169169
}
170170
}
171-
172-
fn toto(&self) -> Vec<IfState> {
173-
self.default.toto()
174-
}
175171
}
176172

177173
impl IncludeLocator for StatsContext {

src/lexer/lexer.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ mk_maps! {
191191
"_Imaginary", Imaginary,
192192
"import", Import,
193193
"inline", Inline,
194+
"__inline", UInline,
195+
"__inline__", UInlineU,
194196
"int", Int,
195197
"long", Long,
196198
"module", Module,
@@ -413,6 +415,8 @@ pub enum Token {
413415
Imaginary,
414416
Import,
415417
Inline,
418+
UInline,
419+
UInlineU,
416420
Int,
417421
Long,
418422
Module,

src/lexer/preprocessor/context.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ pub trait PreprocContext: Default + IncludeLocator {
6969
fn save_switch(&self, file: FileId, pos: usize, next: Position);
7070

7171
fn new_with_if_cache(if_cache: Arc<IfCache>) -> Self;
72-
73-
fn toto(&self) -> Vec<IfState> {
74-
Vec::new()
75-
}
7672
}
7773

7874
#[derive(Default)]
@@ -281,10 +277,6 @@ impl<IL: IncludeLocator> PreprocContext for Context<IL> {
281277
buffer: None,
282278
}
283279
}
284-
285-
fn toto(&self) -> Vec<IfState> {
286-
self.if_stack.clone()
287-
}
288280
}
289281

290282
impl<IL: IncludeLocator> IncludeLocator for Context<IL> {

src/parser/declarations/array.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
// http://opensource.org/licenses/MIT>, at your option. This file may not be
44
// copied, modified, or distributed except according to those terms.
55

6+
use termcolor::StandardStreamLock;
7+
68
use crate::lexer::{TLexer, Token};
79
use crate::parser::attributes::{Attributes, AttributesParser};
10+
use crate::parser::dump::Dump;
811
use crate::parser::errors::ParserError;
912
use crate::parser::expressions::{ExprNode, ExpressionParser};
1013
use crate::parser::types::Type;
@@ -16,14 +19,32 @@ pub struct Dimension {
1619
pub attributes: Option<Attributes>,
1720
}
1821

22+
impl Dump for Dimension {
23+
fn dump(&self, name: &str, prefix: &str, last: bool, stdout: &mut StandardStreamLock) {
24+
dump_obj!(self, name, "", prefix, last, stdout, size, attributes);
25+
}
26+
}
27+
1928
pub type Dimensions = Vec<Dimension>;
2029

30+
impl Dump for Dimensions {
31+
fn dump(&self, name: &str, prefix: &str, last: bool, stdout: &mut StandardStreamLock) {
32+
dump_vec!(name, self, "dim", prefix, last, stdout);
33+
}
34+
}
35+
2136
#[derive(Clone, Debug, PartialEq)]
2237
pub struct Array {
2338
pub base: Option<Type>,
2439
pub dimensions: Dimensions,
2540
}
2641

42+
impl Dump for Array {
43+
fn dump(&self, name: &str, prefix: &str, last: bool, stdout: &mut StandardStreamLock) {
44+
dump_obj!(self, name, "array", prefix, last, stdout, base, dimensions);
45+
}
46+
}
47+
2748
pub struct ArrayParser<'a, L: TLexer> {
2849
lexer: &'a mut L,
2950
}

src/parser/declarations/class.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,6 @@ mod tests {
386386

387387
use super::*;
388388
use crate::lexer::{preprocessor::context::DefaultContext, Lexer};
389-
use crate::mk_var;
390389
use crate::parser::declarations::{self, *};
391390
use crate::parser::expressions::{self, *};
392391
use crate::parser::initializer::Initializer;

src/parser/declarations/specifier.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl Specifier {
9393
*self |= Specifier::TYPEDEF;
9494
true
9595
}
96-
Token::Inline => {
96+
Token::Inline | Token::UInline | Token::UInlineU => {
9797
*self |= Specifier::INLINE;
9898
true
9999
}
@@ -173,6 +173,8 @@ impl Specifier {
173173
match tok {
174174
Token::Typedef
175175
| Token::Inline
176+
| Token::UInline
177+
| Token::UInlineU
176178
| Token::Virtual
177179
| Token::Explicit
178180
| Token::Friend

0 commit comments

Comments
 (0)