import * as vscode from 'vscode' ; enum CommentType { SingleLine , MultiLine } export interface SourceCode { document : vscode . TextDocument ; fullSource ? : string ; sourceWithoutComments ? : string ; sourceWithoutStrings ? : string ; } export class SourceParser { public getSourceCode ( document ) { const source = document . getText ( ) ; const code = { document : document , fullSource : source , sourceWithoutComments : '' , sourceWithoutStrings : '' } ; let prevChar = '' ; let nextChar = '' ; let thisChar = '' ; let inString = false ; let inComment = false ; let commentType = null ; let stringChar = null ; let charWOComments ; let charWOStrings ; for ( let i = 0 ; i < source . length ; i ++ ) { thisChar = source [ i ] ; nextChar = source [ i + 1 ] ; prevChar = source [ i - 1 ] ; charWOComments = thisChar ; charWOStrings = thisChar ; switch ( thisChar ) { case '/' : if ( ! inString ) { if ( ! inComment && nextChar === '/' || prevChar === '/' ) { inComment = true ; commentType = CommentType . SingleLine ; charWOComments = ' ' ; charWOStrings = ' ' ; } else if ( ! inComment && nextChar === '*' ) { inComment = true ; commentType = CommentType . MultiLine ; charWOComments = ' ' ; charWOStrings = ' ' ; } else if ( inComment && commentType === CommentType . MultiLine && prevChar === '*' ) { inComment = false ; commentType = null ; charWOComments = ' ' ; charWOStrings = ' ' ; } else if ( inComment ) { charWOComments = ' ' ; charWOStrings = ' ' ; } } else { charWOStrings = ' ' ; } break ; case '\n' : if ( inComment && commentType === CommentType . SingleLine ) { inComment = false ; commentType = null ; } break ; case '"' : case '\'' : if ( ! inComment ) { charWOStrings = ' ' ; if ( stringChar === thisChar && inString && prevChar !== '~' ) { inString = false ; stringChar = null ; } else if ( stringChar === null && ! inString && ! inComment ) { inString = true ; stringChar = thisChar ; } } else { charWOComments = ' ' ; charWOStrings = ' ' ; } break ; default : if ( inComment ) { charWOComments = ' ' ; charWOStrings = ' ' ; } else if ( inString ) { charWOStrings = ' ' ; } break ; } code . sourceWithoutComments += charWOComments ; code . sourceWithoutStrings += charWOStrings ; } return code ; } }