<commit_before>/*
 * Copyright (c) 2017. tangzx(<EMAIL>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tang.intellij.lua.psi.parser

import com.intellij.lang.PsiBuilder
import com.tang.intellij.lua.parser.LuaParser
import com.tang.intellij.lua.psi.LuaTypes.*

object LuaDeclarationParser {
    fun parseTableFieldList(b: PsiBuilder, l: Int): Boolean {
        val result = parseTableField(b, l)
        while (result != null) {
            val sep = parseTableSep(b, l)
            val nextField = parseTableField(b, l)
            if (sep == null || nextField == null)
                break
        }
        return true
    }

    private fun parseTableSep(b: PsiBuilder, l: Int): PsiBuilder.Marker? {
        when (b.tokenType) {
            SEMI, COMMA -> {
                val mark = b.mark()
                b.advanceLexer()
                mark.done(TABLE_FIELD_SEP)
                return mark
            }
        }
        return null
    }

    private fun parseTableField(b: PsiBuilder, l: Int): PsiBuilder.Marker? {
        when (b.tokenType) {
            LBRACK -> { // '[' expr ']' '=' expr
                val m = b.mark()
                b.advanceLexer()
                val expr = LuaParser.expr(b, l + 1)
                if (expr && b.tokenType == RBRACK) {
                    b.advanceLexer()
                    if (b.tokenType == ASSIGN) {
                        b.advanceLexer()
                        if (LuaParser.expr(b, l + 1)) {
                            m.done(TABLE_FIELD)
                            return m
                        }
                    }
                }
            }
            ID -> { // ID '=' expr
                val m = b.mark()
                b.advanceLexer()
                if (b.tokenType == ASSIGN) {
                    b.advanceLexer()
                    if (LuaExpressionParser.parseExpr(b, l + 1) == null) {
                        b.error("Expression expected")
                    }
                    m.done(TABLE_FIELD)
                    return m
                }
                m.rollbackTo()
            }<commit_msg>bug fix<commit_after>/*
 * Copyright (c) 2017. tangzx(<EMAIL>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tang.intellij.lua.psi.parser

import com.intellij.lang.PsiBuilder
import com.tang.intellij.lua.psi.LuaTypes.*

object LuaDeclarationParser {
    fun parseTableFieldList(b: PsiBuilder, l: Int): Boolean {
        val result = parseTableField(b, l)
        while (result != null) {
            val sep = parseTableSep(b, l)
            val nextField = parseTableField(b, l)
            if (sep == null || nextField == null)
                break
        }
        return true
    }

    private fun parseTableSep(b: PsiBuilder, l: Int): PsiBuilder.Marker? {
        when (b.tokenType) {
            SEMI, COMMA -> {
                val mark = b.mark()
                b.advanceLexer()
                mark.done(TABLE_FIELD_SEP)
                return mark
            }
        }
        return null
    }

    private fun parseTableField(b: PsiBuilder, l: Int): PsiBuilder.Marker? {
        when (b.tokenType) {
            LBRACK -> { // '[' expr ']' '=' expr
                val m = b.mark()
                b.advanceLexer()
                val expr = LuaExpressionParser.parseExpr(b, l + 1)
                if (expr != null) {
                    if (b.tokenType == RBRACK) {
                        b.advanceLexer()
                        if (b.tokenType == ASSIGN) {
                            b.advanceLexer()
                            if (LuaExpressionParser.parseExpr(b, l + 1) == null)
                                b.error("Expression expected")
                        } else b.error("'=' expected")
                    } else b.error("']' expected")
                } else b.error("Expression expected")
                m.done(TABLE_FIELD)

                return m
            }
            ID -> { // ID '=' expr
                val m = b.mark()
                b.advanceLexer()
                if (b.tokenType == ASSIGN) {
                    b.advanceLexer()
                    if (LuaExpressionParser.parseExpr(b, l + 1) == null) {
                        b.error("Expression expected")
                    }
                    m.done(TABLE_FIELD)
                    return m
                }
                m.rollbackTo()
            }
{'fraction_non_alphanumeric': 0.10007371913011426, 'fraction_numerical': 0.004054552156284556, 'mean_word_length': 1.4087882822902795, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2562605', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1447, 'n_words': 415}