function process(arr) {
	let last = undefined

	const result = arr
		.filter(_ => !!_)
		.reduce((acc, item) => {
			const isChild = item.startsWith("\t")
			if (isChild) {
				if (!last) {
					throw `Потомок без родителя:
					${item}`
				}
				last.items.push(item.substr(1))
			} else {
				if (last) {
					last.items = process(last.items)
				}

				const parts = item.split(' ')
				const type = parts.splice(0, 1)[0]
				const value = parts.join(' ')

				last = {
					type,
					value,
					items: []
				}
				acc.push(last)
			}

			return acc
		}, [])

	if (last) {
		last.items = process(last.items)
	}

	return result
}

function parse(str) {
	return process(str.split("\n"))
}

const text =
	`
action ответить на звонок
	when not phone_answered
	do goto phone

action встать
	when not stand
	do
		set stand true
		goto default

action положить трубку
	when on phone
	do
		set phone_answered true
		goto default
`

console.log(JSON.stringify(parse(text), null, 4))
