Skip to content

Commit

Permalink
feat: change line comments prefix from # to //
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard Lavuš committed Oct 26, 2020
1 parent 48fa1bd commit dd9fdc7
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 59 deletions.
40 changes: 20 additions & 20 deletions examples/strict.map.slang
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# https://sfspec.surge.sh/map#sec-Map-Document
// https://sfspec.surge.sh/map#sec-Map-Document
"""
Strict Map

Expand All @@ -8,11 +8,11 @@ Example of the map syntax adhering to the strict syntax.
profile = "http://example.com/profile"
provider = "http://example.com/provider"

# https://sfspec.surge.sh/map#sec-Usecase-Map
// https://sfspec.surge.sh/map#sec-Usecase-Map
"Map Foo
Description of the map Foo"
map Foo {
# https://sfspec.surge.sh/map#sec-Set-Variables
// https://sfspec.surge.sh/map#sec-Set-Variables

set if (!cond) {
foo = 1
Expand All @@ -27,21 +27,21 @@ map Foo {
foo = 1
"foo.bar".bar = call Op()

# https://sfspec.surge.sh/map#sec-Operation-Call
// https://sfspec.surge.sh/map#sec-Operation-Call

call Op(foo = 1, bar = 1 + 1) if (cond) {
# https://sfspec.surge.sh/map#SetOutcome
# https://sfspec.surge.sh/map#SetMapOutcome
// https://sfspec.surge.sh/map#SetOutcome
// https://sfspec.surge.sh/map#SetMapOutcome

# https://sfspec.surge.sh/map#MapResult
// https://sfspec.surge.sh/map#MapResult
map result if (cond) {
foo = 1
}
return map result if (cond) {
"foo" = 1
}

# https://sfspec.surge.sh/map#sec-Map-Error
// https://sfspec.surge.sh/map#sec-Map-Error
map error if (cond) {
"foo.bar" = 1
}
Expand All @@ -50,56 +50,56 @@ map Foo {
}
}

# https://sfspec.surge.sh/map#HTTPCall
// https://sfspec.surge.sh/map#HTTPCall
http GET "/api/{foo}/bar" {
# https://sfspec.surge.sh/map#HTTPSecurity
// https://sfspec.surge.sh/map#HTTPSecurity
security apikey header {
name = "apikey-header"
}

# https://sfspec.surge.sh/map#HTTPRequest
// https://sfspec.surge.sh/map#HTTPRequest
request "application/json" {
# https://sfspec.surge.sh/map#URLQuery
// https://sfspec.surge.sh/map#URLQuery
query {
foo = "hello",
bar = "world"
}

# https://sfspec.surge.sh/map#HTTPHeaders
// https://sfspec.surge.sh/map//HTTPHeaders
headers {
"User-Agent" = "superface v1"
}

# https://sfspec.surge.sh/map#HTTPBody
// https://sfspec.surge.sh/map//HTTPBody
body {
foo = 1,
bar = 1 + 1,
"foo.bar".bar = "3"
}
}

# https://sfspec.surge.sh/map#HTTPRespose
// https://sfspec.surge.sh/map#HTTPRespose
response 300 {
map result {
foo = 1
}
}

# https://sfspec.surge.sh/map#HTTPRespose
// https://sfspec.surge.sh/map#HTTPRespose
response "application/json" {
map error {
foo = 1
}
}

# https://sfspec.surge.sh/map#HTTPRespose
// https://sfspec.surge.sh/map#HTTPRespose
response "*" "en-US" {
return map result {
foo = 1
}
}

# https://sfspec.surge.sh/map#HTTPRespose
// https://sfspec.surge.sh/map#HTTPRespose
response {
return map error {
foo = 1
Expand All @@ -108,9 +108,9 @@ map Foo {
}

http POST "/" {
# https://sfspec.surge.sh/map#HTTPRequest
// https://sfspec.surge.sh/map#HTTPRequest
request {
# https://sfspec.surge.sh/map#HTTPBody
// https://sfspec.surge.sh/map#HTTPBody
body = [1, 2, 3]
}

Expand Down
10 changes: 5 additions & 5 deletions src/language/language.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,11 @@ describe('profile', () => {
f3a
f3b
}
f4 { f4a, f4b boolean } # Ok with comma; however without comma -> error
# f5 f6 f7 # -> error
f8, f9, f10 # -> OK
# f11 string f12 # -> error
f13 string, f14 # -> OK
f4 { f4a, f4b boolean } // Ok with comma; however without comma -> error
// f5 f6 f7 // -> error
f8, f9, f10 // -> OK
// f11 string f12 // -> error, missing comma
f13 string, f14 // -> OK
}`;

const source = new Source(input);
Expand Down
24 changes: 12 additions & 12 deletions src/language/lexer/lexer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,9 @@ describe('lexer', () => {
test('comments', () => {
const lexer = new Lexer(
new Source(`
# line comment
#
asdf # hi
// line comment
//
asdf // hi
asdf
`),
{
Expand Down Expand Up @@ -407,9 +407,9 @@ ng2"
profile = "https://superface.ai/profiles/superface/Map"
#
# Use cases
#
//
// Use cases
//
'''
Get Map
Expand Down Expand Up @@ -467,19 +467,19 @@ ng2"
}
}
#
# Reusable Models
#
//
// Reusable Models
//
model Map {
mapId
source
sourceUrl
}
#
# Reusable Fields
#
//
// Reusable Fields
//
'Id of the map in the store'
field mapId String
Expand Down
33 changes: 17 additions & 16 deletions src/language/lexer/sublexer/default/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,23 @@ export function tryParseIdentifier(
* Returns `undefined` if the current position cannot contain a comment.
*/
export function tryParseComment(slice: string): ParseResult<CommentTokenData> {
if (util.isCommentChar(slice.charCodeAt(0))) {
const commentSlice = slice.slice(1);
const length = util.countStarting(
char => !util.isNewline(char),
commentSlice
);

return {
isError: false,
data: {
kind: LexerTokenKind.COMMENT,
comment: commentSlice.slice(0, length),
},
relativeSpan: { start: 0, end: length + 1 },
};
} else {
const prefix = tryParseScannerRules(slice, { '//': ['//', util.isAny] });
if (prefix === undefined) {
return undefined;
}

const commentSlice = slice.slice(prefix.length);
const bodyLength = util.countStarting(
char => !util.isNewline(char),
commentSlice
);

return {
isError: false,
data: {
kind: LexerTokenKind.COMMENT,
comment: commentSlice.slice(0, bodyLength),
},
relativeSpan: { start: 0, end: prefix.length + bodyLength },
};
}
2 changes: 1 addition & 1 deletion src/language/lexer/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const enum LexerTokenKind {
LITERAL, // number or boolean
STRING, // string literals
IDENTIFIER, // a-z A-Z _ 0-9
COMMENT, // line comments (# foo)
COMMENT, // line comments (// foo)
NEWLINE, // newline
JESSIE_SCRIPT, // Jessie script
}
Expand Down
5 changes: 0 additions & 5 deletions src/language/lexer/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,6 @@ export function isDecoratorChar(char: number): boolean {
return char === 64;
}

export function isCommentChar(char: number): boolean {
// #
return char === 35;
}

// Keyword scanner checks
export function isAny(_: number): boolean {
return true;
Expand Down

0 comments on commit dd9fdc7

Please sign in to comment.