-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45b6455
commit d2b4f8c
Showing
26 changed files
with
906 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
use std::fmt::Debug; | ||
use std::path::Path; | ||
use std::str::FromStr; | ||
|
||
use clap::ValueEnum; | ||
use tree_sitter::QueryError; | ||
|
||
use super::{CodeQuery, Language, LanguageScoper, TSLanguage, TSQuery}; | ||
use crate::find::Find; | ||
|
||
/// The C language. | ||
pub type C = Language<CQuery>; | ||
/// A query for C. | ||
pub type CQuery = CodeQuery<CustomCQuery, PreparedCQuery>; | ||
|
||
/// Prepared tree-sitter queries for C. | ||
#[derive(Debug, Clone, Copy, ValueEnum)] | ||
pub enum PreparedCQuery { | ||
/// Comments (single- and multi-line). | ||
Comments, | ||
/// Strings. | ||
Strings, | ||
/// Includes. | ||
Includes, | ||
/// Type definitions. | ||
TypeDef, | ||
/// `enum` definitions. | ||
Enum, | ||
/// `struct` type definitions. | ||
Struct, | ||
/// Variable definitions. | ||
Variable, | ||
/// Function definitions. | ||
FunctionDef, | ||
/// Function declaration. | ||
FunctionDecl, | ||
/// `switch` blocks. | ||
Switch, | ||
/// `if` blocks. | ||
If, | ||
/// `for` blocks. | ||
For, | ||
/// `while` blocks. | ||
While, | ||
/// `do` blocks. | ||
Do, | ||
/// `union` blocks. | ||
Union, | ||
/// Identifier. | ||
Identifier, | ||
/// Declaration. | ||
Declaration, | ||
/// Call expression. | ||
CallExpression, | ||
} | ||
|
||
impl From<PreparedCQuery> for TSQuery { | ||
fn from(value: PreparedCQuery) -> Self { | ||
Self::new( | ||
&C::lang(), | ||
match value { | ||
PreparedCQuery::Comments => "(comment) @comment", | ||
PreparedCQuery::Strings => "[(string_literal) (system_lib_string)] @string", | ||
PreparedCQuery::Includes => "(preproc_include) @include", | ||
PreparedCQuery::TypeDef => "(type_definition) @typedef", | ||
PreparedCQuery::Enum => "(enum_specifier) @enum", | ||
PreparedCQuery::Struct => "(struct_specifier) @struct", | ||
PreparedCQuery::Variable => "(declaration) @var", | ||
PreparedCQuery::FunctionDef => "(function_definition) @function_definition", | ||
PreparedCQuery::FunctionDecl => "(function_declarator) @function_decl", | ||
PreparedCQuery::Switch => "(switch_statement) @switch", | ||
PreparedCQuery::If => "(if_statement) @if", | ||
PreparedCQuery::For => "(for_statement) @for", | ||
PreparedCQuery::While => "(while_statement) @while", | ||
PreparedCQuery::Union => "(union_specifier) @union", | ||
PreparedCQuery::Do => "(do_statement) @do", | ||
PreparedCQuery::Identifier => "(identifier) @ident", | ||
PreparedCQuery::Declaration => "(declaration) @decl", | ||
PreparedCQuery::CallExpression => "(call_expression) @call", | ||
}, | ||
) | ||
.expect("Prepared queries to be valid") | ||
} | ||
} | ||
|
||
/// A custom tree-sitter query for C. | ||
#[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
pub struct CustomCQuery(String); | ||
|
||
impl FromStr for CustomCQuery { | ||
type Err = QueryError; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match TSQuery::new(&C::lang(), s) { | ||
Ok(_) => Ok(Self(s.to_string())), | ||
Err(e) => Err(e), | ||
} | ||
} | ||
} | ||
|
||
impl From<CustomCQuery> for TSQuery { | ||
fn from(value: CustomCQuery) -> Self { | ||
Self::new(&C::lang(), &value.0) | ||
.expect("Valid query, as object cannot be constructed otherwise") | ||
} | ||
} | ||
|
||
impl LanguageScoper for C { | ||
fn lang() -> TSLanguage { | ||
tree_sitter_c::LANGUAGE.into() | ||
} | ||
|
||
fn pos_query(&self) -> &TSQuery { | ||
&self.positive_query | ||
} | ||
|
||
fn neg_query(&self) -> Option<&TSQuery> { | ||
self.negative_query.as_ref() | ||
} | ||
} | ||
|
||
impl Find for C { | ||
fn extensions(&self) -> &'static [&'static str] { | ||
&["c", "h"] | ||
} | ||
|
||
fn is_path_invalid(&self, _: &Path) -> bool { | ||
false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ use crate::scoping::{ | |
view::ScopedViewBuilder, | ||
}; | ||
|
||
/// C. | ||
pub mod c; | ||
/// C#. | ||
pub mod csharp; | ||
/// Go. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include <stdio.h> | ||
#include "base.h" | ||
|
||
typedef unsigned int uint; | ||
typedef void (*callback)(void); | ||
|
||
/* Multiline comment. | ||
* New line. | ||
*/ | ||
struct S { | ||
int a; | ||
int b; | ||
const char *c; | ||
callback cb; | ||
}; | ||
|
||
union U { | ||
char test[4]; | ||
int a; | ||
}; | ||
|
||
/** | ||
* Doxygen comment | ||
* | ||
*/ | ||
enum E { | ||
A, ///< Doxygen comment. | ||
B, /*< Doxygen comment. */ | ||
C, | ||
}; | ||
|
||
|
||
extern int external_var; | ||
|
||
const char *external_function_declaration(const void *ptr); | ||
|
||
// Main function. | ||
int main(void) { | ||
int a = 0; /* C Stype comments */ | ||
struct S s; | ||
struct S *sp; | ||
union U u; | ||
|
||
// Call a function. | ||
printf("Hello, World!\n"); | ||
s.cb(); | ||
sp->cb(); | ||
|
||
if (a) { | ||
printf("a\n"); | ||
} else if (sp) { | ||
printf("b\n"); | ||
} else { | ||
printf("c\n"); | ||
} | ||
|
||
for (int a = 0; a < 10; a++) { | ||
printf("for\n"); | ||
} | ||
|
||
while (a++ < 100) { | ||
printf("while\n"); | ||
} | ||
|
||
do { | ||
printf("do-while\n"); | ||
} while (0); | ||
|
||
switch (a) { | ||
case 1: | ||
return 0; | ||
default: | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.