Bladeren bron

Permit DocComment at the begining of the file

Ethereum Solidity permits this, although the comment itself is
discarded.

Signed-off-by: Sean Young <sean@mess.org>
Sean Young 4 jaren geleden
bovenliggende
commit
04a076b21a
5 gewijzigde bestanden met toevoegingen van 21 en 15 verwijderingen
  1. 2 2
      src/parser/pt.rs
  2. 11 11
      src/parser/solidity.lalrpop
  3. 2 2
      src/sema/mod.rs
  4. 3 0
      tests/ewasm.rs
  5. 3 0
      tests/imports_testcases/import.sol

+ 2 - 2
src/parser/pt.rs

@@ -35,8 +35,8 @@ pub struct SourceUnit(pub Vec<SourceUnitPart>);
 #[derive(Debug, PartialEq)]
 pub enum SourceUnitPart {
     ContractDefinition(Box<ContractDefinition>),
-    PragmaDirective(Identifier, StringLiteral),
-    ImportDirective(Import),
+    PragmaDirective(Vec<DocComment>, Identifier, StringLiteral),
+    ImportDirective(Vec<DocComment>, Import),
     EnumDefinition(Box<EnumDefinition>),
     StructDefinition(Box<StructDefinition>),
     EventDefinition(Box<EventDefinition>),

+ 11 - 11
src/parser/solidity.lalrpop

@@ -18,8 +18,8 @@ pub SourceUnit: SourceUnit = {
 
 SourceUnitPart: SourceUnitPart = {
     ContractDefinition => SourceUnitPart::ContractDefinition(<>),
-    PragmaDirective => SourceUnitPart::PragmaDirective(<>.0, <>.1),
-    ImportDirective => SourceUnitPart::ImportDirective(<>),
+    PragmaDirective => <>,
+    ImportDirective => <>,
     EnumDefinition => SourceUnitPart::EnumDefinition(<>),
     StructDefinition => SourceUnitPart::StructDefinition(<>),
     EventDefinition => SourceUnitPart::EventDefinition(<>),
@@ -28,21 +28,21 @@ SourceUnitPart: SourceUnitPart = {
     <l:@L> ";" <r:@R> => SourceUnitPart::StraySemicolon(Loc(file_no, l, r)),
 }
 
-ImportDirective: Import = {
-    "import" <s:StringLiteral> ";" => Import::Plain(s),
-    "import" <s:StringLiteral> "as" <id:Identifier> ";" => Import::GlobalSymbol(s, id),
-    "import" "*" "as" <id:Identifier> <from:Identifier> <s:StringLiteral> ";" =>? {
+ImportDirective: SourceUnitPart = {
+    <doc:DocComments> "import" <s:StringLiteral> ";" => SourceUnitPart::ImportDirective(doc, Import::Plain(s)),
+    <doc:DocComments> "import" <s:StringLiteral> "as" <id:Identifier> ";" =>  SourceUnitPart::ImportDirective(doc, Import::GlobalSymbol(s, id)),
+    <doc:DocComments> "import" "*" "as" <id:Identifier> <from:Identifier> <s:StringLiteral> ";" =>? {
         if from.name != "from" {
             Err(ParseError::User { error: LexicalError::ExpectedFrom(from.loc.0, from.loc.1, from.name)})
         } else {
-            Ok(Import::GlobalSymbol(s, id))
+            Ok(SourceUnitPart::ImportDirective(doc, Import::GlobalSymbol(s, id)))
         }
     },
-    "import" "{" <rename:CommaOne<ImportRename>> "}" <from:Identifier> <s:StringLiteral> ";" =>? {
+    <doc:DocComments> "import" "{" <rename:CommaOne<ImportRename>> "}" <from:Identifier> <s:StringLiteral> ";" =>? {
         if from.name != "from" {
             Err(ParseError::User { error:LexicalError::ExpectedFrom(from.loc.0, from.loc.1, from.name)})
         } else {
-            Ok(Import::Rename(s, rename))
+            Ok(SourceUnitPart::ImportDirective(doc, Import::Rename(s, rename)))
         }
     }
 }
@@ -52,9 +52,9 @@ ImportRename: (Identifier, Option<Identifier>) = {
     <from:Identifier> "as" <to:Identifier> => (from, Some(to)),
 }
 
-PragmaDirective: (Identifier, StringLiteral) = {
+PragmaDirective: SourceUnitPart = {
     // The lexer does special parsing for String literal; it isn't really a string literal
-    "pragma" <i:Identifier> <s:StringLiteral> ";" => (i, s)
+    <doc:DocComments> "pragma" <i:Identifier> <s:StringLiteral> ";" => SourceUnitPart::PragmaDirective(doc, i, s)
 }
 
 DocComments: Vec<DocComment> = {

+ 2 - 2
src/sema/mod.rs

@@ -92,10 +92,10 @@ fn sema_file(file: &ResolvedFile, resolver: &mut FileResolver, ns: &mut ast::Nam
     // resolve pragmas and imports
     for part in &pt.0 {
         match part {
-            pt::SourceUnitPart::PragmaDirective(name, value) => {
+            pt::SourceUnitPart::PragmaDirective(_, name, value) => {
                 resolve_pragma(name, value, ns);
             }
-            pt::SourceUnitPart::ImportDirective(import) => {
+            pt::SourceUnitPart::ImportDirective(_, import) => {
                 resolve_import(import, Some(file), file_no, resolver, ns);
             }
             _ => (),

+ 3 - 0
tests/ewasm.rs

@@ -1516,6 +1516,9 @@ fn storage_structs() {
     // verified on remix
     let mut runtime = build_solidity(
         r##"
+        /**
+         * This is a doccomment
+         */
         pragma solidity 0;
         pragma experimental ABIEncoderV2;
 

+ 3 - 0
tests/imports_testcases/import.sol

@@ -1 +1,4 @@
+/**
+ * Doc comments should be permitted but ignored
+ */
 import "bar.sol";