Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <[email protected]>
  • Loading branch information
georgelemon committed Jul 8, 2023
1 parent 6d312f5 commit 7ce5dd2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
44 changes: 22 additions & 22 deletions src/toktok/lexutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ proc init*[L: Lexer](lex: typedesc[L]; fileContent: string, allowMultilineString
lex.multiLineStr = allowMultilineStrings
result = lex

proc generateIdentCase*(lex: var Lexer) # defer
proc handleIdentCase*(lex: var Lexer) # defer

proc lexReady*(lex: var Lexer) =
lex.startPos = lex.getColNumber(lex.bufpos)
Expand Down Expand Up @@ -81,16 +81,27 @@ proc skip*(lex: var Lexer) =
lex.wsno = wsno
return

proc setTokenMulti(lex: var Lexer, tokenKind: TokenKind, offset = 0, multichars = 0) =
## Set meta data of the current token and jump to the next one
proc setToken(lex: var Lexer, tokenKind: TokenKind, offset = 1, initPos = - 1) =
## Set meta data for current token
lex.kind = tokenKind
lex.startPos =
if initPos == -1:
lex.getColNumber(lex.bufpos)
else:
initPos
# lex.getColNumber(lex.bufpos) - lex.token.len # dirty fix
inc(lex.bufpos, offset)

proc setTokenGroup(lex: var Lexer, tokenKind: TokenKind, offset = 0, multichars = 0) =
## Set token with multiple characters
skip lex
lex.startPos = lex.getColNumber(lex.bufpos)
var items = 0
var i = 0
if multichars != 0:
while items < multichars:
while i < multichars:
add lex.token, lex.buf[lex.bufpos]
inc lex.bufpos
inc items
inc i
else:
add lex.token, lex.buf[lex.bufpos]
inc lex.bufpos, offset
Expand Down Expand Up @@ -127,11 +138,11 @@ proc handleSpecial(lex: var Lexer) =
else:
lex.setError("Unknown escape sequence: '\\" & lex.buf[lex.bufpos] & "'")

proc next(lex: var Lexer, tkChar: char, offset = 1): bool =
proc next(lex: var Lexer, ch: char, offset = 1): bool =
# Checking next char if is as expected without
# modifying the current buffer
skip lex
result = lex.buf[lex.bufpos + offset] in {tkChar}
result = lex.buf[lex.bufpos + offset] in {ch}

proc next(lex: var Lexer, chars: string): bool =
# Checks next group of chars and determine if is
Expand Down Expand Up @@ -176,17 +187,6 @@ proc nextToSpec(lex: var Lexer, endChar: char, tokenKind: TokenKind, str = "") =
proc nextToSpec(lex: var Lexer, endChar: string, tokenKind: TokenKind) =
lex.nextToSpec(endChar[^1], tokenKind, endChar)

proc setToken(lex: var Lexer, tokenKind: TokenKind, offset = 1, initPos = - 1) =
## Set meta data for current token
lex.kind = tokenKind
lex.startPos =
if initPos == -1:
lex.getColNumber(lex.bufpos)
else:
initPos
# lex.getColNumber(lex.bufpos) - lex.token.len # dirty fix
inc(lex.bufpos, offset)

proc handleNumber(lex: var Lexer) =
lex.startPos = lex.getColNumber(lex.bufpos)
var toFloat: bool
Expand Down Expand Up @@ -234,7 +234,7 @@ proc handleString[T: Lexer](lex: var T) =
break
of NewLines:
if lex.multiLineStr:
add lex.token, "\\\\n"
# add lex.token, "\n"
inc lex.bufpos
else:
lex.setError("EOL reached before end of string")
Expand All @@ -249,7 +249,7 @@ proc handleString[T: Lexer](lex: var T) =
lex.lineNumber = lineno

proc handleCustomIdent*[T: Lexer](lex: var T, kind: TokenKind) =
## Handle variable declarations based the following char sets
## Handle variable declarations based on the following sets:
## ``{'a'..'z', 'A'..'Z', '_', '-'}`` and ``{'0'..'9'}``
lex.startPos = lex.getColNumber(lex.bufpos)
lex.token = ""
Expand Down Expand Up @@ -279,4 +279,4 @@ proc handleIdent(lex: var Lexer) =
inc lex.bufpos
else: break
# skip lex
lex.generateIdentCase() # template defined in toktok
lex.handleIdentCase() # template defined in toktok
30 changes: 18 additions & 12 deletions src/toktok/macroutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ proc newExceptionStmt*(exception: NimNode, msg: NimNode): NimNode =

proc newObject*(id: string, fields: openArrayParams = [], parent = "", public = false): NimNode =
## Create a new object
result = newNimNode nnkTypeSection
# result = newNimNode nnkTypeSection
var fieldDefs = newEmptyNode()
if fields.len != 0:
fieldDefs = newNimNode nnkRecList
Expand All @@ -58,7 +58,7 @@ proc newObject*(id: string, fields: openArrayParams = [], parent = "", public =
let fromParent =
if parent.len != 0: nnkOfInherit.newTree(ident parent)
else: newEmptyNode()
result.add(
result =
nnkTypeDef.newTree(
objectIdent,
newEmptyNode(),
Expand All @@ -68,33 +68,39 @@ proc newObject*(id: string, fields: openArrayParams = [], parent = "", public =
fieldDefs
)
)
)

proc newTupleType*(id: string, fields: openArrayParams, public = false): NimNode =
## Creates a new tuple type
result = newNimNode nnkTypeSection
# result = newNimNode nnkTypeSection
let tupleIdent =
if public: nnkPostfix.newTree(ident "*", ident id)
else: ident id
var tupleIdentDefs = newEmptyNode()
if fields.len != 0:
tupleIdentDefs = nnkTupleTy.newTree()
for field in fields:
tupleIdentDefs.add(
nnkIdentDefs.newTree(
ident field.k,
ident field.t,
newEmptyNode()
if field.k.contains("|"):
var defs = nnkIdentDefs.newTree()
for f in field.k.split("|"):
defs.add(ident(f))
defs.add(ident(field.t))
defs.add(newEmptyNode())
tupleIdentDefs.add(defs)
else:
tupleIdentDefs.add(
nnkIdentDefs.newTree(
ident field.k,
ident field.t,
newEmptyNode()
)
)
)

result.add(
result =
nnkTypeDef.newTree(
tupleIdent,
newEmptyNode(),
tupleIdentDefs
)
)

proc newProc*(id: string, params: openarray[tuple[k,t: string, vt: bool]],
public = false, returnType, body = newEmptyNode()): NimNode =
Expand Down

0 comments on commit 7ce5dd2

Please sign in to comment.