Skip to content

Commit

Permalink
Modularize parser
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Yu committed Dec 12, 2023
1 parent 0c24947 commit d8d29aa
Show file tree
Hide file tree
Showing 20 changed files with 56 additions and 65 deletions.
4 changes: 1 addition & 3 deletions src/compile/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import * as vscode from 'vscode'
import * as path from 'path'
import * as cs from 'cross-spawn'
import { pickRootPath } from '../utils/quick-pick'
import { parser } from '../parse/parser'

import { lw } from '../lw'
import type { ProcessEnv, RecipeStep, Step } from '../types'
import { build as buildRecipe } from './recipe'
Expand Down Expand Up @@ -261,7 +259,7 @@ async function monitorProcess(step: Step, env: ProcessEnv): Promise<boolean> {
})

lw.compile.process.on('exit', (code, signal) => {
const isSkipped = parser.parseLog(stdout, step.rootFile)
const isSkipped = lw.parse.log(stdout, step.rootFile)
if (!step.isExternal) {
step.isSkipped = isSkipped
}
Expand Down
4 changes: 1 addition & 3 deletions src/completion/completer/citation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {trimMultiLineString} from '../../utils/utils'
import {computeFilteringRange} from './completerutils'
import type { IProvider, ICompletionItem, IProviderArgs } from '../latex'

import { parser } from '../../parse/parser'

const logger = lw.log('Intelli', 'Citation')

class Fields extends Map<string, string> {
Expand Down Expand Up @@ -305,7 +303,7 @@ export class Citation implements IProvider {
const newEntry: CiteSuggestion[] = []
const bibtex = fs.readFileSync(fileName).toString()
logger.log(`Parse BibTeX AST from ${fileName} .`)
const ast = await parser.parseBibTeX(bibtex)
const ast = await lw.parse.bib(bibtex)
if (ast === undefined) {
logger.log(`Parsed 0 bib entries from ${fileName}.`)
lw.event.fire(lw.event.FileParsed, fileName)
Expand Down
3 changes: 1 addition & 2 deletions src/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type { FileCache } from '../types'

import * as utils from '../utils/utils'
import { InputFileRegExp } from '../utils/inputfilepath'
import { parser } from '../parse/parser'

const logger = lw.log('Cacher')

Expand Down Expand Up @@ -248,7 +247,7 @@ function refreshCacheAggressive(filePath: string) {
*/
async function updateAST(fileCache: FileCache) {
logger.log(`Parse LaTeX AST: ${fileCache.filePath} .`)
fileCache.ast = await parser.parseLaTeX(fileCache.content)
fileCache.ast = await lw.parse.tex(fileCache.content)
logger.log(`Parsed LaTeX AST: ${fileCache.filePath} .`)
}

Expand Down
8 changes: 3 additions & 5 deletions src/core/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import * as path from 'path'
import { lw } from '../lw'
import { getSurroundingCommandRange, stripText } from '../utils/utils'

import { parser } from '../parse/parser'

const logger = lw.log('Commander')

export async function build(skipSelection: boolean = false, rootFile: string | undefined = undefined, languageId: string | undefined = undefined, recipe: string | undefined = undefined) {
Expand Down Expand Up @@ -412,22 +410,22 @@ export function devParseLog() {
if (vscode.window.activeTextEditor === undefined) {
return
}
parser.parseLog(vscode.window.activeTextEditor.document.getText())
lw.parse.log(vscode.window.activeTextEditor.document.getText())
}

export async function devParseTeX() {
if (vscode.window.activeTextEditor === undefined) {
return
}
const ast = await parser.parseLaTeX(vscode.window.activeTextEditor.document.getText())
const ast = await lw.parse.tex(vscode.window.activeTextEditor.document.getText())
return vscode.workspace.openTextDocument({content: JSON.stringify(ast, null, 2), language: 'json'}).then(doc => vscode.window.showTextDocument(doc))
}

export async function devParseBib() {
if (vscode.window.activeTextEditor === undefined) {
return
}
const ast = await parser.parseBibTeX(vscode.window.activeTextEditor.document.getText())
const ast = await lw.parse.bib(vscode.window.activeTextEditor.document.getText())
return vscode.workspace.openTextDocument({content: JSON.stringify(ast, null, 2), language: 'json'}).then(doc => vscode.window.showTextDocument(doc))
}

Expand Down
4 changes: 1 addition & 3 deletions src/lint/bibtex-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { lw } from '../lw'
import { bibtexFormat, bibtexSort, getBibtexFormatConfig } from './bibtex-formatter/utils'
import type { BibtexEntry } from './bibtex-formatter/utils'

import { parser } from '../parse/parser'

const logger = lw.log('Format', 'Bib')

export {
Expand Down Expand Up @@ -60,7 +58,7 @@ async function formatDocument(document: vscode.TextDocument, sort: boolean, alig
const columnOffset = range ? range.start.character : 0

logger.log('Parse active BibTeX document for AST.')
const ast = await parser.parseBibTeX(document.getText(range))
const ast = await lw.parse.bib(document.getText(range))
if (ast === undefined) {
return []
}
Expand Down
3 changes: 1 addition & 2 deletions src/locate/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as vscode from 'vscode'
import type * as Ast from '@unified-latex/unified-latex-types'
import { lw } from '../lw'
import { argContentToStr } from '../utils/parser'
import { parser } from '../parse/parser'

const logger = lw.log('EnvPair')

Expand Down Expand Up @@ -82,7 +81,7 @@ class CommandPair {
* document.
*/
async function build(document: vscode.TextDocument): Promise<CommandPair[]> {
const ast = await parser.parseLaTeX(document.getText())
const ast = await lw.parse.tex(document.getText())
if (!ast) {
logger.log('Error parsing current document as AST.')
return []
Expand Down
2 changes: 2 additions & 0 deletions src/lw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { server, viewer } from './preview'
import type { locate } from './locate'
import type { lint } from './lint'
import type { outline } from './outline'
import type { parse } from './parse'

import type { Cleaner } from './extras/cleaner'
import type { LaTeXCommanderTreeView } from './extras/activity-bar'
Expand All @@ -34,6 +35,7 @@ export const lw = {
watcher: {} as typeof watcher,
cache: {} as typeof cache,
root: {} as typeof root,
parse: {} as typeof parse,
compile: {} as typeof compile,
viewer: {} as typeof viewer,
server: {} as typeof server,
Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { cache } from './core/cache'
lw.cache = cache
import { root } from './core/root'
lw.root = root
import { parse } from './parse'
lw.parse = parse
import { compile } from './compile'
lw.compile = compile
import { server, viewer } from './preview'
Expand Down
5 changes: 2 additions & 3 deletions src/outline/structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { TeXElement } from '../types'
import { construct as constructLaTeX } from './structure/latex'
import { buildBibTeX } from './structure/bibtex'
import { construct as constructDocTeX } from './structure/doctex'
import { parser } from '../parse/parser'

const logger = lw.log('Structure')

Expand All @@ -16,11 +15,11 @@ export const outline = {
}

lw.onConfigChange(['view.outline.sections', 'view.outline.commands'], async () => {
await parser.reset()
await lw.parse.reset()
lw.cache.paths().forEach(async filePath => {
const ast = lw.cache.get(filePath)?.ast
if (ast) {
await parser.parseArgs(ast)
await lw.parse.args(ast)
}
})
void reconstruct()
Expand Down
3 changes: 1 addition & 2 deletions src/outline/structure/bibtex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as vscode from 'vscode'
import { bibtexParser } from 'latex-utensils'
import { lw } from '../../lw'
import { type TeXElement, TeXElementType } from '../../types'
import { parser } from '../../parse/parser'

import { bibTools } from '../../completion/completer/citation'

Expand All @@ -29,7 +28,7 @@ export async function buildBibTeX(document: vscode.TextDocument): Promise<TeXEle
return []
}
logger.log('Parse active BibTeX document for AST.')
const ast = await parser.parseBibTeX(document.getText())
const ast = await lw.parse.bib(document.getText())
if (ast === undefined) {
return []
}
Expand Down
3 changes: 1 addition & 2 deletions src/outline/structure/doctex.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as vscode from 'vscode'
import { lw } from '../../lw'
import type { TeXElement } from '../../types'
import { parser } from '../../parse/parser'
import { outline } from './latex'


Expand Down Expand Up @@ -47,7 +46,7 @@ function getDoc(content: string) {
}

async function getToC(document: vscode.TextDocument, docContent: string) {
const ast = await parser.parseLaTeX(docContent)
const ast = await lw.parse.tex(docContent)
if (ast === undefined) {
logger.log('Failed parsing LaTeX AST.')
return []
Expand Down
1 change: 1 addition & 0 deletions src/parse/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { parser as parse } from './parser'
76 changes: 38 additions & 38 deletions src/parse/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import * as path from 'path'
import * as workerpool from 'workerpool'
import type * as Ast from '@unified-latex/unified-latex-types'
import type { bibtexParser } from 'latex-utensils'
import type { Worker } from './parserlib/unified'
import { getEnvDefs, getMacroDefs } from './parserlib/unified-defs'
import { bibtexLogParser } from './parserlib/bibtexlog'
import { biberLogParser } from './parserlib/biberlog'
import { latexLogParser } from './parserlib/latexlog'
import type { Worker } from './parser/unified'
import { getEnvDefs, getMacroDefs } from './parser/unified-defs'
import { bibtexLogParser } from './parser/bibtexlog'
import { biberLogParser } from './parser/biberlog'
import { latexLogParser } from './parser/latexlog'

export const parser = {
parseBibTeX,
parseLog,
parseLaTeX,
parseArgs,
bib,
log,
tex,
args,
reset,
dispose
}
Expand All @@ -27,19 +27,19 @@ async function dispose() {
await pool.terminate(true)
}

async function parseLaTeX(content: string): Promise<Ast.Root> {
async function tex(content: string): Promise<Ast.Root> {
return (await proxy).parseLaTeX(content)
}

async function parseArgs(ast: Ast.Root): Promise<void> {
async function args(ast: Ast.Root): Promise<void> {
return (await proxy).parseArgs(ast, getMacroDefs())
}

async function reset() {
return (await proxy).reset(getMacroDefs(), getEnvDefs())
}

async function parseBibTeX(s: string, options?: bibtexParser.ParserOptions): Promise<bibtexParser.BibtexAst> {
async function bib(s: string, options?: bibtexParser.ParserOptions): Promise<bibtexParser.BibtexAst> {
return (await proxy).parseBibTeX(s, options)
}

Expand All @@ -62,52 +62,52 @@ const bibtexPattern = /^This is BibTeX, Version.*$/m
const biberPattern = /^INFO - This is Biber .*$/m

/**
* @param log The log message to parse.
* @param msg The log message to parse.
* @param rootFile The current root file.
* @returns whether the current compilation is indeed a skipped one in latexmk.
*/
function parseLog(log: string, rootFile?: string): boolean {
function log(msg: string, rootFile?: string): boolean {
let isLaTeXmkSkipped = false
// Canonicalize line-endings
log = log.replace(/(\r\n)|\r/g, '\n')
msg = msg.replace(/(\r\n)|\r/g, '\n')

if (log.match(bibtexPattern)) {
bibtexLogParser.parse(log.match(latexmkPattern) ? trimLaTeXmkBibTeX(log) : log, rootFile)
if (msg.match(bibtexPattern)) {
bibtexLogParser.parse(msg.match(latexmkPattern) ? trimLaTeXmkBibTeX(msg) : msg, rootFile)
bibtexLogParser.showLog()
} else if (log.match(biberPattern)) {
biberLogParser.parse(log.match(latexmkPattern) ? trimLaTeXmkBiber(log) : log, rootFile)
} else if (msg.match(biberPattern)) {
biberLogParser.parse(msg.match(latexmkPattern) ? trimLaTeXmkBiber(msg) : msg, rootFile)
biberLogParser.showLog()
}

if (log.match(latexmkPattern)) {
log = trimLaTeXmk(log)
} else if (log.match(texifyPattern)) {
log = trimTexify(log)
if (msg.match(latexmkPattern)) {
msg = trimLaTeXmk(msg)
} else if (msg.match(texifyPattern)) {
msg = trimTexify(msg)
}
if (log.match(latexPattern) || log.match(latexFatalPattern)) {
latexLogParser.parse(log, rootFile)
if (msg.match(latexPattern) || msg.match(latexFatalPattern)) {
latexLogParser.parse(msg, rootFile)
latexLogParser.showLog()
} else if (latexmkSkipped(log)) {
} else if (latexmkSkipped(msg)) {
isLaTeXmkSkipped = true
}

return isLaTeXmkSkipped
}

function trimLaTeXmk(log: string): string {
return trimPattern(log, latexmkLogLatex, latexmkLog)
function trimLaTeXmk(msg: string): string {
return trimPattern(msg, latexmkLogLatex, latexmkLog)
}

function trimLaTeXmkBibTeX(log: string): string {
return trimPattern(log, bibtexPattern, latexmkLogLatex)
function trimLaTeXmkBibTeX(msg: string): string {
return trimPattern(msg, bibtexPattern, latexmkLogLatex)
}

function trimLaTeXmkBiber(log: string): string {
return trimPattern(log, biberPattern, latexmkLogLatex)
function trimLaTeXmkBiber(msg: string): string {
return trimPattern(msg, biberPattern, latexmkLogLatex)
}

function trimTexify(log: string): string {
return trimPattern(log, texifyLogLatex, texifyLog)
function trimTexify(msg: string): string {
return trimPattern(msg, texifyLogLatex, texifyLog)
}


Expand All @@ -116,8 +116,8 @@ function trimTexify(log: string): string {
* If `endPattern` is not found, the lines from the last occurrence of
* `beginPattern` up to the end is returned.
*/
function trimPattern(log: string, beginPattern: RegExp, endPattern: RegExp): string {
const lines = log.split('\n')
function trimPattern(msg: string, beginPattern: RegExp, endPattern: RegExp): string {
const lines = msg.split('\n')
let startLine = -1
let finalLine = -1
for (let index = 0; index < lines.length; index++) {
Expand All @@ -139,8 +139,8 @@ function trimPattern(log: string, beginPattern: RegExp, endPattern: RegExp): str
}


function latexmkSkipped(log: string): boolean {
if (log.match(latexmkUpToDate) && !log.match(latexmkPattern)) {
function latexmkSkipped(msg: string): boolean {
if (msg.match(latexmkUpToDate) && !msg.match(latexmkPattern)) {
latexLogParser.showLog()
bibtexLogParser.showLog()
biberLogParser.showLog()
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions src/preview/math/mathpreviewlib/cursorrenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type * as Ast from '@unified-latex/unified-latex-types'
import { lw } from '../../../lw'
import { TexMathEnv } from './texmathenvfinder'
import type { ITextDocumentLike } from './textdocumentlike'
import { parser } from '../../../parse/parser'
import { findNode } from '../../../language/selection'

const logger = lw.log('Preview', 'Math', 'Cursor')
Expand Down Expand Up @@ -58,7 +57,7 @@ async function findNodeAt(texMath: TexMathEnv, cursorPos: vscode.Position) {
ast = cache.ast
} else {
logger.log(`Parse LaTeX AST from ${texMath.texString} .`)
ast = await parser.parseLaTeX(texMath.texString)
ast = await lw.parse.tex(texMath.texString)
cache.ast = ast
cache.texString = texMath.texString
}
Expand Down

0 comments on commit d8d29aa

Please sign in to comment.