Skip to content

Commit

Permalink
KeywordHandling.scala new
Browse files Browse the repository at this point in the history
The revised code enhances clarity and readability by incorporating comments that explain the functionality of methods and improving formatting. It retains the original logic for keyword handling while ensuring contextual accuracy. Overall, the changes promote maintainability and ease of understanding for future developers.
  • Loading branch information
Thanesh14 authored Oct 18, 2024
1 parent 11f92c0 commit b141741
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package io.joern.rubysrc2cpg.parser

import io.joern.rubysrc2cpg.parser.RubyLexer.*
import io.joern.rubysrc2cpg.parser.RubyLexer._

trait KeywordHandling { this: RubyLexerBase =>

// A map to associate keyword strings with their corresponding token types
val keywordMap: Map[String, Int] = Map(
"LINE__" -> LINE__,
"ENCODING__" -> ENCODING__,
"FILE__" -> FILE__,
"BEGIN_" -> BEGIN_,

"END_" -> END_,
"ALIAS" -> ALIAS,
"AND" -> AND,
Expand Down Expand Up @@ -48,25 +50,29 @@ trait KeywordHandling { this: RubyLexerBase =>
"YIELD" -> YIELD
)

// Check if the previous token is a colon or a dot
private def isPreviousTokenColonOrDot: Boolean = {
val previousToken = previousTokenTypeOrEOF()
previousToken == RubyLexer.DOT || previousToken == RubyLexer.COLON || previousToken == RubyLexer.COLON2
}

// Check if the next token is a colon or a dot
private def isNextTokenColonOrDot: Boolean = {
_input.LA(1) == '.' || _input.LA(1) == ':'
}

// Set the token type based on the current token text
def setKeywordTokenType(): Unit = {
val tokenText = getText
if (tokenText == null) {
return
}

// Determine the correct token type based on keyword rules
if (
isPreviousTokenColonOrDot || (isNextTokenColonOrDot && tokenText.toUpperCase != "SELF") || !keywordMap.contains(
tokenText.toUpperCase
)
isPreviousTokenColonOrDot ||
(isNextTokenColonOrDot && tokenText.toUpperCase != "SELF") ||
!keywordMap.contains(tokenText.toUpperCase)
) {
setType(RubyLexer.LOCAL_VARIABLE_IDENTIFIER)
} else {
Expand Down

0 comments on commit b141741

Please sign in to comment.