Skip to content

Commit

Permalink
Merge pull request #95
Browse files Browse the repository at this point in the history
  • Loading branch information
delasy authored Sep 28, 2023
2 parents c40aeab + 360eb6e commit 1ea4a3a
Show file tree
Hide file tree
Showing 266 changed files with 11,831 additions and 244 deletions.
5 changes: 3 additions & 2 deletions packages/parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ import parse from "the/parser"

## API

### `parse (path: str) File`
### `parse (pathOrCode: str) File`
Parses path into `File` object.

**Parameters**

- `path` - path to parse
- `pathOrCode` - path or code to parse

**Return value**

Parsed path as `File` object.

**Exceptions**

- `ParserError` - thrown if path doesn't exists
- `ParserError` - thrown if path is not a file

**Examples**
Expand Down
9 changes: 8 additions & 1 deletion packages/parser/package.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
name: the/parser
version: 0.3.3
version: 0.4.1
description: The programming language parser
license: MIT
main: src/main

packages:
the/testing: 0.3.0

scripts:
update-parser-tests: the run scripts/update-parser-tests
update-tokenizer-tests: the run scripts/update-tokenizer-tests
36 changes: 36 additions & 0 deletions packages/parser/scripts/update-parser-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*!
* Copyright (c) 2018 Aaron Delasy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import parse, textify from "../src/parser"
import parseTestFile from "../test/utils"

main {
files := fs_scandirSync("./test/parser")
filesLen := files.len

loop i := 0; i < filesLen; i++ {
file := files[i]
if file.slice(-4) != ".txt" { continue }
print(file as str)

result := parseTestFile("./test/parser/" + file)
mut f := parse(result.in)
stdoutCode := textify(ref f)

fileResult := (result.in.empty ? "" : result.in + os_EOL) + result.delimiter + stdoutCode
fs_writeFileSync("./test/parser/" + file, fileResult.toBuffer())
}
}
37 changes: 37 additions & 0 deletions packages/parser/scripts/update-tokenizer-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*!
* Copyright (c) 2018 Aaron Delasy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as Reader from "../src/reader"
import * as Tokenizer from "../src/tokenizer"
import parseTestFile from "../test/utils"

main {
files := fs_scandirSync("./test/tokenizer")
filesLen := files.len

loop i := 0; i < filesLen; i++ {
file := files[i]
if file.slice(-4) != ".txt" { continue }

result := parseTestFile("./test/tokenizer/" + file)
mut reader := Reader.init(result.in)
mut tokenizer := Tokenizer.init(ref reader)
stdoutCode := Tokenizer.stringify(ref tokenizer, allowComments: file.contains("comment"))

fileResult := result.in + os_EOL + result.delimiter + stdoutCode
fs_writeFileSync("./test/tokenizer/" + file, fileResult.toBuffer())
}
}
20 changes: 10 additions & 10 deletions packages/parser/src/errors
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export fn E0136 () str {
}

export fn E0137 () str {
return "E0137 - Expected union sub type after pipe operator"
return "E0137 - Expected union subtype after pipe operator"
}

export fn E0138 (after: str) str {
Expand All @@ -220,9 +220,9 @@ export fn E0140 () str {
return "E0140 - Expected function type parameter type"
}

export fn E0141 () str {
return "E0141 - Expected function type parameter type after parameter name"
}
// export fn E0141 () str {
// return "E0141 - Expected function type parameter type after parameter name"
// }

export fn E0142 () str {
return "E0142 - Expected function type parameter type after colon"
Expand Down Expand Up @@ -256,9 +256,9 @@ export fn E0149 (kw: str) str {
return "E0149 - Expected identifier after `" + kw + "` keyword"
}

export fn E0150 () str {
return "E0150 - Expected closure expression body"
}
// export fn E0150 () str {
// return "E0150 - Expected closure expression body"
// }

export fn E0151 () str {
return "E0151 - Expected if statement consequent statement"
Expand All @@ -268,9 +268,9 @@ export fn E0152 () str {
return "E0152 - Expected if statement alternate statement"
}

export fn E0153 () str {
return "E0153 - Expected main declaration body"
}
// export fn E0153 () str {
// return "E0153 - Expected main declaration body"
// }

export fn E0154 () str {
return "E0154 - Expected equals sign after type alias declaration name"
Expand Down
57 changes: 37 additions & 20 deletions packages/parser/src/expression
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ export fn endsWithObjExpr (self: t.Expression) bool {
} elif self.isConditional() {
selfBody := self.asConditional()
return endsWithObjExpr(selfBody.alternate)
} elif self.isObject() {
selfBody := self.asObject()
return true
} elif self.isReference() {
selfBody := self.asReference()
return endsWithObjExpr(selfBody.expression)
Expand All @@ -43,7 +40,7 @@ export fn endsWithObjExpr (self: t.Expression) bool {
}
}

return false
return self.isObject()
}

export fn extractLastObjExpr (self: t.Expression) t.ObjectExpression {
Expand Down Expand Up @@ -78,46 +75,58 @@ export fn extractLastObjExpr (self: t.Expression) t.ObjectExpression {
export fn subtractLastObjExpr (self: t.Expression) t.Expression {
if self.isAssignment() {
selfBody := self.asAssignment()
right := subtractLastObjExpr(selfBody.right)

return create(t.AssignmentExpression{
left: selfBody.left,
operator: selfBody.operator,
right: subtractLastObjExpr(selfBody.right)
}, self.start, self.end)
right: right
}, self.start, right.end)
} elif self.isAwait() {
selfBody := self.asAwait()
expression := subtractLastObjExpr(selfBody.expression)

return create(t.AwaitExpression{
expression: subtractLastObjExpr(selfBody.expression)
}, self.start, self.end)
expression: expression
}, self.start, expression.end)
} elif self.isBinary() {
selfBody := self.asBinary()
right := subtractLastObjExpr(selfBody.right)

return create(t.BinaryExpression{
left: selfBody.left,
operator: selfBody.operator,
right: subtractLastObjExpr(selfBody.right)
}, self.start, self.end)
right: right
}, self.start, right.end)
} elif self.isConditional() {
selfBody := self.asConditional()
alternate := subtractLastObjExpr(selfBody.alternate)

return create(t.ConditionalExpression{
condition: selfBody.condition,
consequent: selfBody.consequent,
alternate: subtractLastObjExpr(selfBody.alternate)
}, self.start, self.end)
alternate: alternate
}, self.start, alternate.end)
} elif self.isObject() {
selfBody := self.asObject()
return typeToExpression(selfBody.id)
} elif self.isReference() {
selfBody := self.asReference()
expression := subtractLastObjExpr(selfBody.expression)

return create(t.ReferenceExpression{
expression: subtractLastObjExpr(selfBody.expression)
}, self.start, self.end)
expression: expression
}, self.start, expression.end)
} elif self.isUnary() {
selfBody := self.asUnary()
if selfBody.prefix {
operand := subtractLastObjExpr(selfBody.operand)

return create(t.UnaryExpression{
operator: selfBody.operator,
operand: subtractLastObjExpr(selfBody.operand),
operand: operand,
prefix: selfBody.prefix
}, self.start, self.end)
}, self.start, operand.end)
}
}

Expand All @@ -131,15 +140,19 @@ export fn toType (self: t.Expression) t.Type {
return t.Type{
body: t.IdentifierType{name: selfBody},
start: self.start,
end: self.end
end: self.end,
leadingComments: self.leadingComments,
trailingComments: self.trailingComments
}
} elif self.isPropertyAccess() {
selfBody := self.asPropertyAccess()

return t.Type{
body: t.MemberType{t: toType(selfBody.expression), name: selfBody.name},
start: self.start,
end: self.end
end: self.end,
leadingComments: self.leadingComments,
trailingComments: self.trailingComments
}
}

Expand All @@ -153,15 +166,19 @@ export fn typeToExpression (self: t.Type) t.Expression {
return t.Expression{
body: selfBody.name,
start: self.start,
end: self.end
end: self.end,
leadingComments: self.leadingComments,
trailingComments: self.trailingComments
}
} elif self.isMember() {
selfBody := self.asMember()

return t.Expression{
body: t.PropertyAccessExpression{expression: typeToExpression(selfBody.t), name: selfBody.name},
start: self.start,
end: self.end
end: self.end,
leadingComments: self.leadingComments,
trailingComments: self.trailingComments
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/parser/src/main
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import
Type_toText
from "./text"
import Program_traverse from "./program"
import parse, report, stringify from "./parser"
import parse, report from "./parser"

export ArrayType
export FunctionType
Expand Down
Loading

0 comments on commit 1ea4a3a

Please sign in to comment.