errors.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. package compiler
  2. import (
  3. "fmt"
  4. "strings"
  5. "kumachan/standalone/util/richtext"
  6. "kumachan/lang/source"
  7. )
  8. const BlockClassError = "error"
  9. const BlockClassErrorContentItem = "error-content-item"
  10. func makeErrorDescBlankBlock() richtext.Block {
  11. var b richtext.Block
  12. b.AddClass(BlockClassError)
  13. b.WriteSpan("Error: ", richtext.TAG_B)
  14. return b
  15. }
  16. func makeErrorDescBlock(msg ...string) richtext.Block {
  17. var b = makeErrorDescBlankBlock()
  18. for i, span := range msg {
  19. b.WriteSpan(span, (func() string {
  20. if (i % 2) == 0 {
  21. if strings.HasPrefix(span, "(") && strings.HasSuffix(span, ")") {
  22. return richtext.TAG_ERR_NOTE
  23. } else {
  24. return richtext.TAG_ERR
  25. }
  26. } else {
  27. return richtext.TAG_ERR_INLINE
  28. }
  29. })())
  30. }
  31. return b
  32. }
  33. func makeEmptyErrorContentItemBlock() richtext.Block {
  34. var b richtext.Block
  35. b.AddClass(BlockClassErrorContentItem)
  36. return b
  37. }
  38. type E_DuplicateAlias struct {
  39. Name string
  40. }
  41. func (e E_DuplicateAlias) DescribeError() richtext.Block {
  42. if e.Name == "" {
  43. return makeErrorDescBlock (
  44. "duplicate alias",
  45. )
  46. } else {
  47. return makeErrorDescBlock (
  48. "duplicate alias:",
  49. e.Name,
  50. )
  51. }
  52. }
  53. type E_InvalidAlias struct {
  54. Name string
  55. }
  56. func (e E_InvalidAlias) DescribeError() richtext.Block {
  57. if e.Name == "" {
  58. return makeErrorDescBlock (
  59. "invalid alias",
  60. )
  61. } else {
  62. return makeErrorDescBlock (
  63. "invalid alias:",
  64. e.Name,
  65. )
  66. }
  67. }
  68. type E_AliasTargetNotFound struct {
  69. Target string
  70. }
  71. func (e E_AliasTargetNotFound) DescribeError() richtext.Block {
  72. return makeErrorDescBlock (
  73. "alias target not found:",
  74. e.Target,
  75. )
  76. }
  77. type E_DuplicateTypeDecl struct {
  78. Name string
  79. }
  80. func (e E_DuplicateTypeDecl) DescribeError() richtext.Block {
  81. return makeErrorDescBlock (
  82. "duplicate declaration of type",
  83. e.Name,
  84. )
  85. }
  86. type E_DuplicateFunDecl struct {
  87. Name string
  88. Assoc string
  89. }
  90. func (e E_DuplicateFunDecl) DescribeError() richtext.Block {
  91. if ((e.Name == "") && (e.Assoc == "")) {
  92. return makeErrorDescBlock (
  93. "duplicate declaration of entry point in current namespace",
  94. )
  95. } else {
  96. return makeErrorDescBlock (
  97. "duplicate declaration of global name",
  98. fmt.Sprintf("%s (%s)", e.Name, e.Assoc),
  99. "in current namespace",
  100. )
  101. }
  102. }
  103. type E_InvalidFieldName struct {
  104. FieldKind string
  105. FieldName string
  106. }
  107. func (e E_InvalidFieldName) DescribeError() richtext.Block {
  108. return makeErrorDescBlock (
  109. fmt.Sprintf("invalid %s name", e.FieldKind),
  110. e.FieldName,
  111. )
  112. }
  113. type E_DuplicateField struct {
  114. FieldKind string
  115. FieldName string
  116. }
  117. func (e E_DuplicateField) DescribeError() richtext.Block {
  118. return makeErrorDescBlock (
  119. fmt.Sprintf("duplicate %s", e.FieldKind),
  120. e.FieldName,
  121. )
  122. }
  123. type E_GenericEnum struct {}
  124. func (E_GenericEnum) DescribeError() richtext.Block {
  125. return makeErrorDescBlock (
  126. "a generic type cannot be an enum type",
  127. )
  128. }
  129. type E_NoSuchType struct {
  130. TypeRef string
  131. }
  132. func (e E_NoSuchType) DescribeError() richtext.Block {
  133. return makeErrorDescBlock (
  134. "no such type:",
  135. e.TypeRef,
  136. )
  137. }
  138. type E_NoSuchTypeParameter struct {
  139. Name string
  140. }
  141. func (e E_NoSuchTypeParameter) DescribeError() richtext.Block {
  142. return makeErrorDescBlock (
  143. "no such type parameter:",
  144. e.Name,
  145. )
  146. }
  147. type E_NotRecord struct {
  148. TypeRef string
  149. }
  150. func (e E_NotRecord) DescribeError() richtext.Block {
  151. return makeErrorDescBlock (
  152. "type",
  153. e.TypeRef,
  154. "is not a record",
  155. )
  156. }
  157. type E_InvalidRecordTag struct {
  158. Tag string
  159. }
  160. func (e E_InvalidRecordTag) DescribeError() richtext.Block {
  161. return makeErrorDescBlock (
  162. "invalid record tag:",
  163. e.Tag,
  164. )
  165. }
  166. type E_NotInterface struct {
  167. TypeRef string
  168. }
  169. func (e E_NotInterface) DescribeError() richtext.Block {
  170. return makeErrorDescBlock (
  171. "type",
  172. e.TypeRef,
  173. "is not an interface",
  174. )
  175. }
  176. type E_DuplicateInterface struct {
  177. TypeRef string
  178. }
  179. func (e E_DuplicateInterface) DescribeError() richtext.Block {
  180. return makeErrorDescBlock (
  181. "duplicate interface",
  182. e.TypeRef,
  183. )
  184. }
  185. type E_TypeParamsNotIdentical struct {
  186. Concrete string
  187. Interface string
  188. }
  189. func (e E_TypeParamsNotIdentical) DescribeError() richtext.Block {
  190. return makeErrorDescBlock (
  191. "identical type parameters required for type",
  192. e.Concrete,
  193. "to implement interface",
  194. e.Interface,
  195. )
  196. }
  197. type E_MissingMethod struct {
  198. Concrete string
  199. Interface string
  200. Method string
  201. }
  202. func (e E_MissingMethod) DescribeError() richtext.Block {
  203. return makeErrorDescBlock (
  204. "missing method",
  205. e.Method,
  206. "for type",
  207. e.Concrete,
  208. "to implement interface",
  209. e.Interface,
  210. "(note: method should be in the same file)",
  211. )
  212. }
  213. type E_WrongMethodType struct {
  214. Concrete string
  215. Interface string
  216. Method string
  217. Expected string
  218. Actual string
  219. }
  220. func (e E_WrongMethodType) DescribeError() richtext.Block {
  221. var b = makeErrorDescBlock (
  222. "bad method",
  223. e.Method,
  224. "for type",
  225. e.Concrete,
  226. "to implement interface",
  227. e.Interface,
  228. )
  229. b.Append(makeErrorDescBlock (
  230. "expect method type to be",
  231. e.Expected,
  232. "but got",
  233. e.Actual,
  234. ))
  235. return b
  236. }
  237. type E_MethodNameUnavailable struct {
  238. Name string
  239. }
  240. func (e E_MethodNameUnavailable) DescribeError() richtext.Block {
  241. return makeErrorDescBlock (
  242. "unavailable method name",
  243. e.Name,
  244. "(conflicts with record field or abstract method)",
  245. )
  246. }
  247. type E_TypeArgsWrongQuantity struct {
  248. Type string
  249. Given int
  250. Required int
  251. }
  252. func (e E_TypeArgsWrongQuantity) DescribeError() richtext.Block {
  253. return makeErrorDescBlock (
  254. fmt.Sprintf(
  255. "expect %d type argument(s) but given %d for type",
  256. e.Required,
  257. e.Given,
  258. ),
  259. e.Type,
  260. )
  261. }
  262. type E_CannotMatchRecord struct {
  263. TypeDesc string
  264. }
  265. func (e E_CannotMatchRecord) DescribeError() richtext.Block {
  266. return makeErrorDescBlock (
  267. "cannot match record from type",
  268. e.TypeDesc,
  269. )
  270. }
  271. type E_RecordSizeNotMatching struct {
  272. PatternArity int
  273. RecordSize int
  274. RecordDesc string
  275. }
  276. func (e E_RecordSizeNotMatching) DescribeError() richtext.Block {
  277. return makeErrorDescBlock (
  278. "cannot match record",
  279. e.RecordDesc,
  280. fmt.Sprintf(
  281. "(size not matching: pattern(%d) / record(%d))",
  282. e.PatternArity, e.RecordSize,
  283. ),
  284. )
  285. }
  286. type E_DuplicateBinding struct {
  287. BindingName string
  288. }
  289. func (e E_DuplicateBinding) DescribeError() richtext.Block {
  290. return makeErrorDescBlock (
  291. "duplicate binding:",
  292. e.BindingName,
  293. )
  294. }
  295. type E_UnusedBinding struct {
  296. BindingName string
  297. }
  298. func (e E_UnusedBinding) DescribeError() richtext.Block {
  299. return makeErrorDescBlock (
  300. "unused binding:",
  301. e.BindingName,
  302. )
  303. }
  304. type E_LambdaAssignedToIncompatibleType struct {
  305. TypeDesc string
  306. }
  307. func (e E_LambdaAssignedToIncompatibleType) DescribeError() richtext.Block {
  308. return makeErrorDescBlock (
  309. "lambda cannot be assigned to incompatible type",
  310. e.TypeDesc,
  311. )
  312. }
  313. type E_ExpectExplicitTypeCast struct {}
  314. func (E_ExpectExplicitTypeCast) DescribeError() richtext.Block {
  315. return makeErrorDescBlock("expect explicit type cast")
  316. }
  317. type E_ExpectSufficientTypeArguments struct {}
  318. func (E_ExpectSufficientTypeArguments) DescribeError() richtext.Block {
  319. return makeErrorDescBlock("expect sufficient type arguments")
  320. }
  321. type E_NotAssignable struct {
  322. From string
  323. To string
  324. }
  325. func (e E_NotAssignable) DescribeError() richtext.Block {
  326. return makeErrorDescBlock (
  327. "cannot assign from", e.From,
  328. "to", e.To,
  329. )
  330. }
  331. type E_AmbiguousAssignmentToUnion struct {
  332. Union string
  333. Key1 string
  334. Key2 string
  335. }
  336. func (e E_AmbiguousAssignmentToUnion) DescribeError() richtext.Block {
  337. return makeErrorDescBlock (
  338. "ambiguous assignment to union type", e.Union,
  339. "as", e.Key1,
  340. "and", e.Key2,
  341. "are both assignable items",
  342. )
  343. }
  344. type E_NoSuchFieldOrMethod struct {
  345. FieldName string
  346. TypeDesc string
  347. }
  348. func (e E_NoSuchFieldOrMethod) DescribeError() richtext.Block {
  349. return makeErrorDescBlock (
  350. "no such field/method",
  351. e.FieldName,
  352. "on type",
  353. e.TypeDesc,
  354. )
  355. }
  356. type E_InteriorRefUnavailable struct {
  357. InteriorRef string
  358. TypeDesc string
  359. }
  360. func (e E_InteriorRefUnavailable) DescribeError() richtext.Block {
  361. return makeErrorDescBlock (
  362. "interior reference",
  363. e.InteriorRef,
  364. "is unavailable for type",
  365. e.TypeDesc,
  366. )
  367. }
  368. type E_InvalidFloat struct {}
  369. func (E_InvalidFloat) DescribeError() richtext.Block {
  370. return makeErrorDescBlock (
  371. "invalid floating-point number",
  372. )
  373. }
  374. type E_InvalidText struct {}
  375. func (e E_InvalidText) DescribeError() richtext.Block {
  376. return makeErrorDescBlock (
  377. "invalid text",
  378. )
  379. }
  380. type E_InvalidChar struct {}
  381. func (e E_InvalidChar) DescribeError() richtext.Block {
  382. return makeErrorDescBlock (
  383. "invalid character",
  384. )
  385. }
  386. type E_InvalidRegexp struct {
  387. Detail string
  388. }
  389. func (e E_InvalidRegexp) DescribeError() richtext.Block {
  390. return makeErrorDescBlock (
  391. "invalid regular expression",
  392. fmt.Sprintf("(%s)", e.Detail),
  393. )
  394. }
  395. type E_TooManyTypeArgs struct {}
  396. func (e E_TooManyTypeArgs) DescribeError() richtext.Block {
  397. return makeErrorDescBlock (
  398. "too many type arguments",
  399. )
  400. }
  401. type E_MissingOperatorParameter struct {}
  402. func (E_MissingOperatorParameter) DescribeError() richtext.Block {
  403. return makeErrorDescBlock (
  404. "operator must have at least one parameter",
  405. )
  406. }
  407. type E_OperatorFirstParameterHasDefaultValue struct {}
  408. func (E_OperatorFirstParameterHasDefaultValue) DescribeError() richtext.Block {
  409. return makeErrorDescBlock (
  410. "operator cannot have default value on first parameter",
  411. )
  412. }
  413. type E_MissingVariadicParameter struct {}
  414. func (E_MissingVariadicParameter) DescribeError() richtext.Block {
  415. return makeErrorDescBlock (
  416. "variadic function/operator must have at least one parameter",
  417. )
  418. }
  419. type E_InvalidVariadicParameter struct {}
  420. func (E_InvalidVariadicParameter) DescribeError() richtext.Block {
  421. return makeErrorDescBlock (
  422. "variadic function/operator must have a final list-parameter",
  423. )
  424. }
  425. type E_DuplicateArgument struct {
  426. Name string
  427. }
  428. func (e E_DuplicateArgument) DescribeError() richtext.Block {
  429. return makeErrorDescBlock (
  430. "duplicate argument",
  431. e.Name,
  432. )
  433. }
  434. type E_MissingArgument struct {
  435. Name string
  436. }
  437. func (e E_MissingArgument) DescribeError() richtext.Block {
  438. return makeErrorDescBlock (
  439. "missing argument:",
  440. e.Name,
  441. )
  442. }
  443. type E_SuperfluousArgument struct {}
  444. func (E_SuperfluousArgument) DescribeError() richtext.Block {
  445. return makeErrorDescBlock (
  446. "superfluous argument",
  447. )
  448. }
  449. type E_UnableToInferDefaultValueType struct {
  450. ArgName string
  451. }
  452. func (e E_UnableToInferDefaultValueType) DescribeError() richtext.Block {
  453. return makeErrorDescBlock (
  454. "unable to infer the type of default value of argument",
  455. e.ArgName,
  456. )
  457. }
  458. type E_UnableToInferVaType struct {}
  459. func (E_UnableToInferVaType) DescribeError() richtext.Block {
  460. return makeErrorDescBlock (
  461. "unable to infer variadic argument type",
  462. )
  463. }
  464. type E_NoSuchThing struct {
  465. Ref string
  466. }
  467. func (e E_NoSuchThing) DescribeError() richtext.Block {
  468. return makeErrorDescBlock (
  469. "no such thing:",
  470. e.Ref,
  471. )
  472. }
  473. type E_NoSuchFunction struct {
  474. FunKindDesc string
  475. FunNameDesc string
  476. }
  477. func (e E_NoSuchFunction) DescribeError() richtext.Block {
  478. return makeErrorDescBlock (
  479. fmt.Sprintf("no such %s:", e.FunKindDesc),
  480. e.FunNameDesc,
  481. )
  482. }
  483. type E_NotCallable struct {
  484. TypeDesc string
  485. }
  486. func (e E_NotCallable) DescribeError() richtext.Block {
  487. return makeErrorDescBlock (
  488. "cannot call a value of type",
  489. e.TypeDesc,
  490. )
  491. }
  492. type E_LambdaCallWrongArgsQuantity struct {
  493. Given int
  494. Required int
  495. }
  496. func (e E_LambdaCallWrongArgsQuantity) DescribeError() richtext.Block {
  497. return makeErrorDescBlock (
  498. fmt.Sprintf("expect %d argument(s) but given %d", e.Required, e.Given),
  499. )
  500. }
  501. type E_LambdaCallUnorderedArgs struct {}
  502. func (E_LambdaCallUnorderedArgs) DescribeError() richtext.Block {
  503. return makeErrorDescBlock (
  504. "cannot call a lambda using unordered arguments",
  505. )
  506. }
  507. type E_InvalidConstructorUsage struct {}
  508. func (E_InvalidConstructorUsage) DescribeError() richtext.Block {
  509. return makeErrorDescBlock (
  510. "invalid constructor usage",
  511. )
  512. }
  513. type E_SuperfluousTypeArgs struct {}
  514. func (E_SuperfluousTypeArgs) DescribeError() richtext.Block {
  515. return makeErrorDescBlock (
  516. "superfluous type arguments",
  517. )
  518. }
  519. type E_CannotAssignFunctionRequiringImplicitInput struct {}
  520. func (E_CannotAssignFunctionRequiringImplicitInput) DescribeError() richtext.Block {
  521. return makeErrorDescBlock (
  522. "cannot assign a function/operator that requires implicit input",
  523. )
  524. }
  525. type E_UnableToUseAsLambda struct {
  526. InOutDesc string
  527. }
  528. func (e E_UnableToUseAsLambda) DescribeError() richtext.Block {
  529. return makeErrorDescBlock (
  530. "unable to use as lambda: unqualified signature",
  531. e.InOutDesc,
  532. )
  533. }
  534. type E_MultipleAssignable struct {
  535. Ref string
  536. }
  537. func (e E_MultipleAssignable) DescribeError() richtext.Block {
  538. return makeErrorDescBlock (
  539. "multiple things available for assignment of",
  540. e.Ref,
  541. )
  542. }
  543. type E_NoneAssignable struct {
  544. Ref string
  545. Details [] NoneAssignableErrorDetail
  546. }
  547. type NoneAssignableErrorDetail struct {
  548. ItemName string
  549. ErrorContent source.ErrorContent
  550. }
  551. func (e E_NoneAssignable) DescribeError() richtext.Block {
  552. var b = makeErrorDescBlock (
  553. "nothing qualified for assignment of",
  554. e.Ref,
  555. )
  556. for _, detail := range e.Details {
  557. var desc = detail.ErrorContent.DescribeError()
  558. desc = desc.WithoutLeadingSpan()
  559. desc = desc.WithLeadingSpan((detail.ItemName + ":"), richtext.TAG_B)
  560. b.Append(desc)
  561. }
  562. return b
  563. }
  564. type E_InvalidCondType struct {
  565. TypeDesc string
  566. }
  567. func (e E_InvalidCondType) DescribeError() richtext.Block {
  568. return makeErrorDescBlock (
  569. "cannot use type",
  570. e.TypeDesc,
  571. "as a condition",
  572. )
  573. }
  574. type E_InvalidCondPattern struct {}
  575. func (E_InvalidCondPattern) DescribeError() richtext.Block {
  576. return makeErrorDescBlock (
  577. "invalid pattern",
  578. )
  579. }
  580. type E_InvalidWhenOperand struct {
  581. TypeDesc string
  582. }
  583. func (e E_InvalidWhenOperand) DescribeError() richtext.Block {
  584. return makeErrorDescBlock (
  585. "invalid when expression operand type",
  586. e.TypeDesc,
  587. )
  588. }
  589. type E_InvalidEachOperand struct {
  590. TypeDesc string
  591. }
  592. func (e E_InvalidEachOperand) DescribeError() richtext.Block {
  593. return makeErrorDescBlock (
  594. "invalid each expression operand",
  595. e.TypeDesc,
  596. )
  597. }
  598. type E_InvalidCasePattern struct {}
  599. func (E_InvalidCasePattern) DescribeError() richtext.Block {
  600. return makeErrorDescBlock (
  601. "invalid pattern",
  602. )
  603. }
  604. type E_NoSuchCase struct {
  605. CaseName string
  606. }
  607. func (e E_NoSuchCase) DescribeError() richtext.Block {
  608. return makeErrorDescBlock (
  609. "no such case:",
  610. e.CaseName,
  611. )
  612. }
  613. type E_DuplicateCase struct {
  614. CaseName string
  615. }
  616. func (e E_DuplicateCase) DescribeError() richtext.Block {
  617. return makeErrorDescBlock (
  618. "duplicate case",
  619. e.CaseName,
  620. )
  621. }
  622. type E_MissingCase struct {
  623. CaseName string
  624. }
  625. func (e E_MissingCase) DescribeError() richtext.Block {
  626. return makeErrorDescBlock (
  627. "non-exhaustive cases: missing case",
  628. e.CaseName,
  629. )
  630. }
  631. type E_SuperfluousDefaultCase struct {}
  632. func (E_SuperfluousDefaultCase) DescribeError() richtext.Block {
  633. return makeErrorDescBlock (
  634. "superfluous default case",
  635. )
  636. }