Skip to content

Commit

Permalink
Language server code complete (hyperledger-solang#1560)
Browse files Browse the repository at this point in the history
Signed-off-by: Govardhan G D <[email protected]>
  • Loading branch information
chioni16 authored Nov 25, 2023
1 parent 2967e4e commit 99ecfc0
Show file tree
Hide file tree
Showing 36 changed files with 1,060 additions and 455 deletions.
4 changes: 4 additions & 0 deletions solang-parser/src/helpers/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1937,6 +1937,7 @@ mod tests {
} => "event name() anonymous;",

pt::FunctionDefinition {
loc_prototype: loc!(),
ty: pt::FunctionTy::Function,
name: Some(id("name")),
name_loc: loc!(),
Expand All @@ -1947,6 +1948,7 @@ mod tests {
body: None,
} => "function name();",
pt::FunctionDefinition {
loc_prototype: loc!(),
ty: pt::FunctionTy::Function,
name: Some(id("name")),
name_loc: loc!(),
Expand All @@ -1957,6 +1959,7 @@ mod tests {
body: Some(stmt!({})),
} => "function name() {}",
pt::FunctionDefinition {
loc_prototype: loc!(),
ty: pt::FunctionTy::Function,
name: Some(id("name")),
name_loc: loc!(),
Expand All @@ -1967,6 +1970,7 @@ mod tests {
body: Some(stmt!({})),
} => "function name() returns (uint256) {}",
pt::FunctionDefinition {
loc_prototype: loc!(),
ty: pt::FunctionTy::Function,
name: Some(id("name")),
name_loc: loc!(),
Expand Down
2 changes: 2 additions & 0 deletions solang-parser/src/pt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,8 @@ pub enum FunctionTy {
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub struct FunctionDefinition {
/// The function prototype location.
pub loc_prototype: Loc,
/// The code location.
pub loc: Loc,
/// The function type.
Expand Down
14 changes: 9 additions & 5 deletions solang-parser/src/solidity.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -686,11 +686,12 @@ returns: Option<Loc> = {
ModifierDefinition: Box<FunctionDefinition> = {
<l:@L> "modifier" <nl:@L> <name:SolIdentifier> <nr:@R> <params:ParameterList?>
<attributes:FunctionAttribute*>
<returns:(returns ParameterList)?> <r:@R> <body:BlockStatementOrSemiColon> => {
<returns:(returns ParameterList)?> <pr:@R> <body:BlockStatementOrSemiColon> <r:@R> => {
let params = params.unwrap_or(Vec::new());
let (return_not_returns, returns) = returns.unwrap_or((None, Vec::new()));

Box::new(FunctionDefinition{
loc_prototype: Loc::File(file_no, l, pr),
loc: Loc::File(file_no, l, r),
ty: FunctionTy::Modifier,
name: Some(name),
Expand All @@ -707,10 +708,11 @@ ModifierDefinition: Box<FunctionDefinition> = {
ConstructorDefinition: Box<FunctionDefinition> = {
<l:@L> <ty:FunctionTy> <nl:@L> <name:SolIdentifier?> <nr:@R> <params:ParameterList>
<attributes:FunctionAttribute*>
<returns:(returns ParameterList)?> <r:@R> <body:BlockStatementOrSemiColon> => {
<returns:(returns ParameterList)?> <pr:@R> <body:BlockStatementOrSemiColon> <r:@R> => {
let (return_not_returns, returns) = returns.unwrap_or((None, Vec::new()));

Box::new(FunctionDefinition{
loc_prototype: Loc::File(file_no, l, pr),
loc: Loc::File(file_no, l, r),
ty,
name,
Expand All @@ -727,10 +729,11 @@ ConstructorDefinition: Box<FunctionDefinition> = {
FunctionDefinition: Box<FunctionDefinition> = {
<l:@L> "function" <nl:@L> <name:SolIdentifierOrError> <nr:@R> <params:ParameterList>
<attributes:FunctionAttribute*>
<returns:(returns ParameterList)?> <r:@R> <body:BlockStatementOrSemiColon> => {
<returns:(returns ParameterList)?> <pr:@R> <body:BlockStatementOrSemiColon> <r:@R> => {
let (return_not_returns, returns) = returns.unwrap_or((None, Vec::new()));

Box::new(FunctionDefinition{
loc_prototype: Loc::File(file_no, l, pr),
loc: Loc::File(file_no, l, r),
ty: FunctionTy::Function,
name,
Expand All @@ -744,16 +747,17 @@ FunctionDefinition: Box<FunctionDefinition> = {
},
// Old-style fallback function without name. Sema will give a nice error message
// with some instructions how to update your syntax
<l:@L> <ft:FunctionType> <r:@R> <body:BlockStatementOrSemiColon> => {
<l:@L> <ft:FunctionType> <pr:@R> <body:BlockStatementOrSemiColon> <r:@R> => {
match ft {
// we're dropping the trailing attributes, but this production is for
// generating an error messages
Type::Function { params, attributes, returns, .. } => {
Box::new(FunctionDefinition{
loc_prototype: Loc::File(file_no, l, pr),
loc: Loc::File(file_no, l, r),
ty: FunctionTy::Function,
name: None,
name_loc: Loc::File(file_no, l, r),
name_loc: Loc::File(file_no, l, pr),
params,
attributes,
return_not_returns: None,
Expand Down
12 changes: 8 additions & 4 deletions solang-parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ fn parse_test() {
],
})),
SourceUnitPart::FunctionDefinition(Box::new(FunctionDefinition {
loc: Loc::File(0, 720, 735),
loc_prototype: Loc::File(0, 720, 735),
loc: Loc::File(0, 720, 1147),
ty: FunctionTy::Function,
name: Some(Identifier {
loc: Loc::File(0, 729, 732),
Expand Down Expand Up @@ -587,7 +588,8 @@ fn test_assembly_parser() {

let expected_parse_tree = SourceUnit(vec![SourceUnitPart::FunctionDefinition(Box::new(
FunctionDefinition {
loc: Loc::File(0, 17, 32),
loc_prototype: Loc::File(0, 17, 32),
loc: Loc::File(0, 17, 1045),
ty: FunctionTy::Function,
name: Some(Identifier {
loc: Loc::File(0, 26, 29),
Expand Down Expand Up @@ -1040,7 +1042,8 @@ fn parse_revert_test() {
fields: vec![],
})),
ContractPart::FunctionDefinition(Box::new(FunctionDefinition {
loc: Loc::File(0, 73, 89),
loc_prototype: Loc::File(0, 73, 89),
loc: Loc::File(0, 73, 140),
ty: FunctionTy::Function,
name: Some(Identifier {
loc: Loc::File(0, 82, 85),
Expand Down Expand Up @@ -1293,7 +1296,8 @@ contract MyTest {
base: vec![],
parts: vec![ContractPart::FunctionDefinition(
FunctionDefinition {
loc: File(0, 23, 55),
loc_prototype: File(0, 23, 55),
loc: File(0, 23, 57),
ty: FunctionTy::Constructor,
name: None,
name_loc: File(0, 34, 34),
Expand Down
Loading

0 comments on commit 99ecfc0

Please sign in to comment.